Android: Stop playback on Home or Back button - android

When I press back or home in my application, it hides, but the MediaPlayer keeps going. Is there a way to know when these buttons have been pressed and stop playback before closing?

just implement this method in your class where media player is calling...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{ //Back key pressed
//Things to Do
if(mediaPlayer!= null)
{
mediaPlayer.stop();
mediaPlayer=null;
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}

You should be able to override the onPause() function in your Activity. onPause() will be called when you Activity is hidden and you can pause the MediaPlayer there.
For example,
#Override
protected void onPause() {
super.onPause(); // Don't forget this line
myMediaPlayer.pause() // Or whatever the function is to pause it
}

Don't forget to add super.onPause; if you forget it will cause a FC.

Related

When MediaPlayer is preparing to start, i want go back to "activity main" but my application is crash

When MediaPlayer is preparing to start, i want go back to "activity main" but my application is crash.
I try this code, but not working :s
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
seekHandler.removeCallbacks(run, null);
}
}
return super.onKeyDown(keyCode, event);
}
Just a hunch, but try removing your seek callbacks before calling release.
You should add logcat output and the code where the error occurs to get better answers.

Stop music player on device home button android

I have an android application in which am try to play a background sound for an particular activity(sound will play on single activity not for an whole application).
Am using this code to start the MediaPlayer
MediaPlayer backMP = MediaPlayer.create(this, R.raw.theme_loop);
backMP.setLooping(true);
backMP.start();
It is working fine but I just want to stop the music on home button press for this I have try
backMP.release(), backMp.stop() in onPause() method nothing is work for me.
use backMP.stop in both onDestroy,onStop,and onPause it will solve your problem
use this way
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.d("Test", "Home button pressed!");
backMP.stop();
}
return super.onKeyDown(keyCode, event);
}
or you can use like that start playing in onResume()
#Override
protected void onResume() {
super.onResume();
backMP.start();
}
and stop in onpouse() like that
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
backMP.stop();
}

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

Media Player streaming, when Exit app music should stop

I currently have a mediaplayer streaming some mp3 files. What I would like to do is if the User presses the home button to exit the app, the music stops playing. how would I go about doing this?
In your app' onPause() or onDestroy() call mediaPlayer.stop().
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "home button pressed"); }
return super.onKeyDown(keyCode, event); }
Something like this, except instead of log, do whatever you do in your implementation to kill the service/asynctask/thread handling your music.
Add this method to your class to override your home button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
media.stop();
finish();
//or complete end your application
//System.runFinalizersOnExit(true);
//System.exit(0);
return true;
}
return false;
}

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);
}
}

Categories

Resources