I have a MediaPlayer that streams music in the background, when I press the home button the music stops, which what I want, but when I resume back to the application, the music is not there anymore. And when the current track ends, it doesn't loop.
My code is the following:
MediaPlayer backgroundMusic;
int length = 0;
//play background music
backgroundMusic = MediaPlayer.create(MainActivity.this, R.raw.background_music);
backgroundMusic.start();
backgroundMusic.setLooping(true);
#Override
protected void onPause() {
super.onPause();
if(backgroundMusic.isPlaying()){
backgroundMusic.pause();
length = backgroundMusic.getCurrentPosition();
}else{
return;
}
}
public void onPrepared(MediaPlayer backgroundMusic){
backgroundMusic.start();
backgroundMusic.seekTo(length);
}
How can I resume the music from where it stopped, when I press the Home Button, and Also make the music loop.
Thanks in advance.
Try to implement your code with service class if you want your player to play even in background or if you want to store the position save your data in static variable in onpause and restore it onResume of your Activity.
Preserve position of playing in shared preferences and not in local variable
Related
My activity is playing mp3 file while is active, and my intention is to pause it while app takes user to another activity and resume when this activity is again active. Solution that was here around seems to be the right one but unfortunately id doesn't work, the audio file every time starts from beginning. My code is obvious:
private int length;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(this, R.raw.bensound_thejazzpiano);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(0.4f,0.4f);
mediaPlayer.start();
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.stop();
length = mediaPlayer.getCurrentPosition();
}
#Override
protected void onResume() {
super.onResume();
mediaPlayer.seekTo(length);
mediaPlayer.start();
}
I would also be satisfied with methods that mutes sound and restores the volume after goin back to activity (and muted tune can go on in background), but replacing start/stop methods with setVolume also doesn't provide to any results...
Maybe there are wrong methods I've overridden?
You have to "pause"(not stop) the mediaplayer in onPause, then start in onResume. That worked for me.
I am using a MediaPlayer inside a Fragment, I have managed to stop the music from playing, whenever the current fragment is changed.
But the problem is that, if I was within the fragment, and I press the back button to close the application, the music keeps streaming.
My code is the following:
#Override
public void setUserVisibleHint (boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
If (this.isVisible ()){
If (!isVisibleToUser){
sound.stop ();
}else {
sound.start ();
}
}
}
sound = MediaPlayer.create(getActivity
(), R.raw.music);
How can I stop the music from playing, when I exit the application?
You can stop your MediaPlayer from streaming in onPause() method of the fragment.
This will stop the mediaplayer when you open the other fragment or you destroy the fragment
I only want to have background music in my first activity (MainActivity). When I Change to the next activity after clicking on a button, I want the music to stop. Is the the following code is enough, or do I have to implement the button, too?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(MainActivity.this, R.raw.music);
mp.setLooping(true);
mp.start();
add the onPause() method in your first activity, and pause/stop the music :)
#Override
public void onPause() {
if(mp.isPlaying()){
mp.pause();
//mp.stop();
}
super.onPause();
}
and even you can use the onResume() method to start again the music.
#Override
protected void onResume() {
mp.start();
super.onResume();
}
You'll need to make sure to stop the music when another activity launches. If you want it to start again if they return to this activity, you'll need to code that, likely by using startActivityForResult and activating it on result. Do you want it to play even if they hit the home button and hide the activity? If not, the easiest thing to do it start it in onResume and pause it in onPause.
i create an app, and in one layout (just call it layout A), i play the media player, then when i went to another layout (just call it Layout B), i want the sound from the Layout A is continue playing in Layout B, and when i went back to the Layout A, i also want the media player is still continuing the music that was played before.
In Layout A, i set this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
if(!isMuted())
{
player.setLooping(true);
player.start();
}
...
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
stop=1;
startActivity(intent);
}
});
And this code:
#Override
protected void onPause()
{
super.onPause();
if (stop!=1)
{
//stop=1 if i go to another layout, for example when i want to go to Layout B
//if the device is automatically locked, i want the media player is paused and it resumed when i unlock the device, so i use stop!=1 to know whether the sound should be paused or not
player.pause();
length = player.getCurrentPosition();
}
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
//isMuted is method to know whether the sound is muted or not, if it isn't muted, then the sound is resumed
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
In layout B, i used this code in onCreate:
player = MediaPlayer.create(this, R.raw.sound);
....
And this code:
protected void onPause()
{
super.onPause();
player.pause();
length = player.getCurrentPosition();
}
#Override
protected void onResume()
{
super.onResume();
if(!isMuted())
{
if(player.isPlaying())
{
}
else
{
player.seekTo(length);
player.setLooping(true);
player.start();
}
}
}
But this is the problem.
When i went to Layout B, the Media Player from Layout A and Media Player from Layout B is played in the same time, so the sound is played simultaneously at one time.
When i went back to the Layout A, the Media Player in Layout B is stopped and the Media Player in Layout A is stopped and played from the beginning again, it didn't continue the Media Player that was played before.
When the device is locked, the media player is still played although i have used the indicator whether is should be paused or not. Any correction to the code?
I would recommend that you use a Service to play and pause/stop your music. It is not recommended to use Activity to handle music that way. You can start a Service and then play music in it. Services run in background and don't get destroyed automatically too often as compared to Activity. Here's a sample app code that does similar to what you need media player sound continue in all activities
In Activity MediaPlayer can play far.
You should use Service to play MediaPlayer and control from anywhere in application.
I used this tutorial.
https://thenewcircle.com/s/post/60/servicesdemo_using_android_service
I'm new to android so I'm having a problem thinking of a solution. First thing I do is change activities to a different screen, and while in the activity I download a song. I then return to MainActivity which is a MediaPlayer activity. Now, I'm a little confused on activity lifecycles, if I return to MainActivity, and run:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File song = new File(path, "SpaceJam.mp3");
That's in my onCreate, so basically I'm asking does onCreate run every time you return to an activity?
Second, I have a play/pause button:
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// check for already playing
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
// Changing button image to play button
btnPlay.setImageResource(R.drawable.play_t); }
}else{
// Resume song
if(mp!=null){
mp.start();
// Changing button image to pause button
btnPlay.setImageResource(R.drawable.pause_t);
}
}
}
});
The problem is, I don't want to do use:
mp.setDataSource(path+"/"+fileName);
mp.prepare();
mp.start();
On my play button, because it'll reinitialize the song every time. What's the best way to go about doing this?
Thanks
That's in my onCreate, so basically I'm asking does onCreate run every time you return to an activity?
No. onResume() or onStart() would be good choices but you should read the Activity life cycle documentation and choose accordingly.
http://developer.android.com/training/basics/activity-lifecycle/index.html
On my play button, because it'll reinitialize the song every time. What's the best way to go about doing this?
To save the current position, e.g. in onPause()
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
To resume, e.g. in onResume()
Mediaplayer.seekTo(length);
Mediaplayer.start();