I am working on a music player and everything is fine the play, stop, and pause buttons.
My problem is that whenever a song is playing and I press the back button and reopen the app the buttons won't work with the current song.
This happens as well when I change the orientation. Is there a way I can save the state of the media player and then obtain the state so that I can stop the song that is being played?
Well, you could use
.getCurrentPosition()
to get how many milliseconds into thew song you are, then use
.seekTo(int milliseconds)
upon resuming or an orientation change, to get to the same spot.
You can use the SeekTo method.
Seek To
Then add:
protected void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putInt(PLAY_TIME, mMediaPlayer.getCurrentPosition());
}
And later in the OnCreate, you just need to check something like this:
if(savedInstanceState != null && savedInstanceState.containsKey(PLAY_TIME)) {
mMediaPlayer.seekTo(savedInstanceState.getInt(PLAY_TIME, 0));
}
Hope this helps
Related
I am playing video on video view, the issue which i am facing is that when user press back button, the application gets stuck for some time and then onStop method is called of that activity, i don't know why the application is taking time in calling onStop method. Please can anyone help me out.
What jaydroider said is right. You have to override onBackPressed method as follows
#Override
public void onBackPressed() {
<stop your video here>
super.onBackPressed();
}
It is important that you stop the video before calling super.onBackPressed(); so that your application does not get stuck.
You should stop video on onPause() not on onBackPressed(). Because user may use the Home key while using your app. This will stop playing video whenever your screen is not visible to user.
I have a music player
app I created, while Im playing music and exit the app by pressing home or back button and then launch the app again, it doesn't return to the current state where I exit the app and also the song index goes back to its default value which is 1 but the music is still playing. and when I play music again, the current song I played is still playing. therefore, there are two songs are playing. How can I save the current state of my application?
You can save the states in onPause() and can retrieve them back in onresume()
Or you can use onwindowFocusChanged() :
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
//retrive state
else
//save state
}
Short answer: You really oughta need to read this activity page which tells you about the life cycle of an activity..
Long answer : what's going on is all your variables are reloading (probaly through onResume or onStart) so you lose state. You can easily save the state of some variable using onSaveInstanceState(Bundle b) link
Then reload your variables using onLoadInstanceState.. Easy as that..
More details can be found in this answer;
https://stackoverflow.com/a/151940/137404
es you can save your current state . just add the following code in your menifest file .
<activity android:name=".Song_list"
android:configChanges="keyboardHidden|orientation|screenSize"
//the line below will solve your problem just put it in every activity that you created
android:launchMode="singleTop">
</activity>
In Android, When I press back/home button the music stops (that's fine), but when I re-enter to the activity I want to begin/resume playing back the music.
How does Android manage playback of audio when the program goes into the background?
The code given below only works if the activity hasn't been destroyed. If you want to survive when the user presses the back button, you'll have to write the current status of the media player out to a file, and reload it when the activity starts again.
I'm not sure that's the correct behavior though. After the back button is pressed, your application SHOULD terminate, and start up next time in a reset state. I think. If the back button gets used for navigation in your app, the solution is to put up an alert asking of the user REALLY REALLY wants to quit, on the final back button that terminates the app. Override
Activity.onBackPressed to customize the behavior of the back button.
Note also that your activity can be destroyed even in paused state, if the system is looking for more memory, if the screen flips, or if several other high-level system states change. To deal with this case, you need to implement savedInstanceState handling. Implement Activity.onSaveInstanceState, and then look for a non-null savedInstanceState parameter in your implementation of onCreate.
In both cases, you're going to have to implement some method for recreating the media player, preparing the audio again, and seeking to the correct position in the resurrected audio.
Ordinarily, you should include a lot more details and code about your problem, but I just happened to see your other post on a related matter.
You need the following in onResume(). Note, if you are doing mp.start() in onCreate(), you can remove that start as onResume() is called after onCreate().
#Override
protected void onResume() {
if(mp != null && !mp.isPlaying())
mp.start();
super.onResume();
}
start() from MeidaPlayer will start over if stop or never played, or from where paused if it was paused.
add this method,
#Override
protected void onResume() {
super.onResume();
if(mp != null)
mp.start();
}
I am building media player application in that on First Activity I am Showing All Song List and on song click i am playing song in media player. But when i back pressed i come to the song list again and again when i back pressed i come out of the application. I want that song should be continue to play when i come out of the application and when i resume to my application it continues to play.
Plz Suggest me some solution.
I have tried this code :
#Override
public void onBackPressed() {
// do whatever you desire
moveTaskToBack(true);
return;
}
But this is not working. When i resume to my application it throws error.
in the songsplay list activity, after the startactivity(intent) write finish();
it will finish() the activity, now we can press back button songsplay list activity will not appear.
i'm using media player to display .m3u8 streaming in my application,
when pressing Home button ,,or when the screen Goes off..i want to pause the media then when the user return to it ..it start playing..
I tried onPause and onResume to pause and start the Media player..
But it Gives a nullPointerExc on the onResume Method.
In onPause, save whatever metadata you need in order to reload the media into the MediaPlayer (file location, current position, etc).
In onResume, check the MediaPlayer instance for null. If it's null, reload your media from this information. Where you save this data is up to you, but SharedPreferences is probably the easiest to get started with.