I've tried overriding onPause and everything but the mp3 file continues to play even after the home button is pressed.
I have logged the onPause method and it does seem to be working, however it is not stopping the media player.
I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. Is there anyway to stop the music when the home button is pressed?
User870380 is correct, the home key cannot be overridden, and for good reason. The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. Your code will probably look something like this:
#Override
protected void onPause() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
You may also need to check to see if it throws an IllegalStateException. The MediaPlayer does that. A lot.
Are you getting any errors in your logcat when you try to stop the MediaPlayer?
Are you running the MediaPlayer in a service?
Related
I want to start an activity in the onStop() method without the activity being loaded popping up on the screen which will disturb the user, I want it to just load the activity with the onstop() method in the background so that when user return to the app later on, user could see the activity being loaded, is there any way to make this possible?
I have read so many of the answers about this matter and tried LifeCycleObserver but the intent still pop up, I want it to stay in the background until user open the app again. Thank you in advance
in your_Application class create variable:
Boolean stateOn = false;
in your onStop() set value true,
like
MyApplication.stateOn = true;
in onDestroy() make it false again, (before calling super.onDestroy())
MyApplication.stateOn = false;
and in on onResume() check it, if it was true , start another activity
if (MyApplication.stateOn) {
startActivity({intent});
}
you can crate your_AppCompatActivity class and do above code in there, to appling this config in all activities who extends your_AppCompatActivity
Following on from previous question which it appears that no one can provide a solution, is it possible to reset the mediaplayer automatically, I do not want the user to press buttons to reset, when new activity starts.
Currently it appears that it is not.
if (mediaplayer!= null) {
mediaplayer.reset();
mediaplayer.release();
mediaplayer= null;
}
Apply this code in below activity:
1)this code will apply on destroy of previous activity/pause of this activity.
2)on create method of new activity.(using intent)
I am trying to use the Toggle Button to play a sound when the button is "on" and stop the sound when the button is "off".
This seems to only work when I first toggle between the on/off button.
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
sound1.start();
} else {
// The toggle is disabled
sound1.stop();
}
}
});
Because you called method stop().
There is the description of the method:
Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.
Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.
Calling stop() has no effect on a MediaPlayer object that is already in the Stopped state.
How to play exactly right,you just need to visit this website of google. click me.
So the best thing that you can is to use a class constant to check if the flag is already set. You will need to save this to the savedCacheInstance when onPause/onStop/OnDestroy and then reload it. Something like:
boolean isMusicPlaying = false;
If you save this using savedInstanceCache in both onCreate & onStop/onDestroy, you can control the playback between rotations and start ups/exits. The class property is also inside the class scope of the listener so that any action you take, should update the class property. The best way to identify any issue is to of course, use the debugger. That way you can control the play functions and see which is breaking.
Hello. It is a bit difficult to make you understand the situation I am stuck in. I tried to find answers about this here on StackOverflow but it didn't help so here is the APK file attached.
https://www.dropbox.com/s/r5mengjfe4moxug/JustAnotherMusicPlayer.apk
In the app, when the music is being played and then I press the back button the activity is destroyed. And then when I try to access that activity again, from a notification, the music stops and the information that was displayed is also lost. How can I overcome this problem?
impliment this method and start the same activity again
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
// start your activity here
}
I've been using this code and it plays sound just fine on Froyo and Gingerbread (and I assume Honeycomb as well as my friends have used it):
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
Button clicker = (Button) findViewById(R.id.clicker);
clicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
mp.start();
}
});
The audio I'm using is in WAV format. I've checked it to make sure it's not corrupted and it's fine. This code and sound file still run correctly on Gingerbread.
On Ice Cream Sandwich and JellyBean devices (a galaxy nexus and a nexus 7 respectively) this code does not work. No sound is played. There's nothing put in logcat. I've searched through the Internet and asked my friends for ideas and I can't come up with anything.
Thank you in advance for your time!
Wrap the call in an IllegalStateException, run it thru the debugger and see what you are getting. Also set a boolean isPlaying=mp.isPlaying(); and check its value. Also try a mp.reset() before starting and see it it works.
Also see http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29
Go ahead and inplement MediaPlayer.OnErrorListener and register the method with the media player. See what error you get.