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();
}
Related
I have a button which leads to open a Preference Screen, hence I have not released Media Player object in onPause().
But this leads to problem of running background music when clicks on Home button in Android Emulator.
take variable before oncreate
boolean pref_clicked=false;
onclick of button just write pref_clicked=true;
and
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if(!pref_clicked)
//pause your music player
}
#Override
public void onStop() {
super.onStop();
//pause your music player
}
I'm having some problems after making an override of the method onDestroy.
My app is a music player, using the instance of mediaplayer I need at some point to force the release of it if no music is playing.
This is my code so far, for making the trick I've both overrided the onKeyDown() and onDestroy() method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mp.isPlaying())
{
//Genera la notifica
generateNotificationSong();
//Muovi in background
moveTaskToBack(true);
}
else finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
//Faccio un override della funzione onDestroy per evitare che il mediaplayer continui
//a mandare musica in background, inoltre l'UpdateTimeTask risulta inutile
#Override
public void onDestroy()
{
mNotify.cancel(001);
if(mHandler != null)
mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
if(mp != null)
mp.release(); //rilascio il media player
super.onDestroy();
}
That's it, now when I want to close the application I simply press the back button and the apps calls the methods onPause() onStop() and onDestroy() right?
Anyway sometimes happens that after the closing the phone freeze for 4-5 seconds and a message is displayed: "The program Application has been closed".
I know I'm doing something wrong here but I don't know what, I need some help.
Thanks in advice!
super.onDestroy() must be the first call of the onDestroy method if you override it.
Try the below code it works for me
#Override
public void onDestroy() {
mediaPlayer.stop();
super.onDestroy();
}
}
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
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;
}
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.