Changing the physical camera button control in android - android

public boolean onKeyDown(int keycode,KeyEvent event)
{
//check if the camera button is pressed
if(keycode==KeyEvent.KEYCODE_CAMERA)
{
//if result
if (mResultView.IsShowingResult)
{
mResultView.IsShowingResult = false;
}
else if (mCameraReadyFlag == true)//switch to camera view
{
mCameraReadyFlag = false;
mPreview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
return true;
}
return super.onKeyDown(keycode, event);
}
I have the following code to check if the camera button is checked and if it is,then take a picture.But it uses the physical camera button code.I want to take a picture if the screen is pressed since camera buttons are now out of fashion.Can you give me equivalent function for the same please.

Related

Disable volume up/down to open camera

I have an app using firebase scanning to scan barcodes. I want to disable the use of volume up/down to open the scanner/camera. I also want the user to be able to still change the volume using those button.
I tried overwriting the onKeyDown event on my base activity but it doesn't fix my issue.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return false;
} else {
return super.onKeyDown(keyCode, event);
}
}

Block hardware camera shutter / capture button Samsung Galaxy Camera on EK-GC200

I want to use my own camera modul at the Samsung Galaxy Camera EK-GC200.
I can get a keycode for both buttons, but capture button always opens his own camera intent which then of course collapes with my own camera modul.
Also zoom buttons always show some slide-popup when used.
Meanwhile I found some topics that some people were able to block the HOME button on their devices. But seems this is not usable for the camera buttons.
So is there any way to block the hardware buttons so at least the camera capture button doesn't open its own camera intent anymore ?
In your MainActivity.java (or some other activity), paste the following:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.e(TAG, "keyCode: " + keyCode); // If you want to see the keycodes
// If User hits the (physical) shutter button of the EK-GC200 camera
if (KeyEvent.KEYCODE_FOCUS == keyCode || KeyEvent.KEYCODE_CAMERA == keyCode) {
// Do nothing or start your own camera App
return true;
}
return super.onKeyDown(keyCode, event);
}
If you also want to intercept the return button, do:
if ((keyCode == KeyEvent.KEYCODE_BACK )) {
// Upon return / back key:
// Do NOT go to super.onKeyDown(keyCode, event);
return true;
}
The HOME button can not be intercepted in this manner.
Hope this helps.

how to hide a media controller when a video starts in android?

I'm trying to play a video with the media controller sets to invisible, so when I override the back key, the video stops and the activity finishes. But, when I press the back button before spending three seconds, I need press the button twice, because the media controller is activated. So, how to hide the media controller when the video starts?
private void playRecording() {
MediaController mc = new MediaController(this);
video_view.setMediaController(mc);
video_view.setVideoPath(output_file_name);
video_view.start();
mc.show(0);
mc.hide();
}
private void stopPlayingRecording() {
video_view.stopPlayback();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (is_playing) {
stopPlayingRecording();
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Once the video is started you can try.
video_view.setMediaController(null);
then if you want to show it again when a user presses on the screen you could implement a ontouchevent that will create one and show it for a few seconds and then set it back to null again

Android back button and MediaController

I know how to take control of the back button. I have a VideoView embedded in a FrameLayout. My question is when the video pops up, the video controls are present for a few seconds. Hitting the back button while they are visible hides the video controls. Is there a way to ignore that function and do the next back action as if the video controls weren't visible?
The reason I ask is if I really do want to go back, I must hit the back button twice; once to hide the controls and second to actually go back
Based on the source code, this should work:
Extend MediaController (for the purposes of this answer, call it RonnieMediaController)
Override dispatchKeyEvent() in RonnieMediaController
Before chaining to the superclass, check for KeyEvent.KEYCODE_BACK, and if that is encountered, tell your activity to finish()
Use RonnieMediaController instead of MediaController with your VideoView
Personally, I'd just leave it alone, as with this change your user cannot make a RonnieMediaController disappear on demand.
You can simply write:
mVideoView.setMediaController(new MediaController(this){
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
((Activity) getContext()).finish();
return super.dispatchKeyEvent(event);
}
});
No need to create new class.
The previous solutions no longer work with Android Pie +, you must instead :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mediaController.addOnUnhandledKeyEventListener((v, event) -> {
//Handle BACK button
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
{
mediaController.hide(); //Hide mediaController,according to your needs, you can also called here onBackPressed() or finish()
}
return true;
});
}
You can also have the Activity handle the event:
mVideoView.setMediaController(new MediaController(this){
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
((Activity) getContext()).onBackPressed();
return true;
}
}
return super.dispatchKeyEvent(event);
}
});
Then handle it in your Activity:
#Override
public void onBackPressed() {
// clean up or send result here
finish();
}
In Xamarin.Android, you can deal with this problem like this
public class CustomMediaController : MediaController
{
private FragmentActivity act;
public CustomMediaController(Context context, FragmentActivity myActivity) : base(context)
{
act = myActivity;
}
public override bool DispatchKeyEvent(KeyEvent e)
{
if(e.KeyCode == Keycode.Back)
{
act.OnBackPressed();
}
return base.DispatchKeyEvent(e);
}
}

android - overridden volume button has affected back button?

Using the code below I have stopped the use of the volume buttons unless I am streaming audio (otherwise it annoyingly changes the ringer volume), but the 'Back' button isn't working.
Pressing 'back' should got to my phones desktop (or exit my app, like you would expect), but it isn't doing anything. If I open the menu, 'Back' will close the menu as it should, but I can't leave the app.
I have copied the code onto other activities within my app, if I open another activity within my app, because the 'Back' button isn't working, I can't go back to the main screen :)
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Suppress the use of the volume keys unless we are currently listening to the stream
if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) {
if(StreamService.INT_PLAY_STATE==0){
return true;
}else{
return false;
}
}
return false;
Why is this happening?
Haven't tested, but I think you need to include an else where you call super.onKeyDown, ie:
if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
code
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
more code
} else {
super.onKeyDown(keyCode, event);
}
Otherwise, you're capturing all keycodes and returning false after checking the volume codes.
A simpler and more robust way to have the volume keys always control the Media volume is to insert this line into your Activity's onCreate():
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Dude, just change the audio context on that activity to media volume:
http://developer.android.com/reference/android/media/AudioManager.html
EDIT:
private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
//your code
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//your code
return true;
} else {
super.onKeyDown(keyCode, event);
}
return true;
}

Categories

Resources