Stop MediaPlayer when Fragment closes - android

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

Related

Resume MediaPlayer after pressing Home Button

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

Why does my MediaPlayer stop working if I "interrupt" it?

I want my button to be "spammable". This means that if I tap on the button repeatedly the MediaPlayer starts all over again.
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (firstTextView.getText().equals("Hello world!")) {
firstTextView.setText("You clicked!");
} else {
firstTextView.setText("Hello world!");
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.start();
}
});
When I interrupt the MediaPlayer when it is playing, it stops and never starts again. Why?
EDIT: The problem is that I called stop(). Thanks for pointing that out.
As per the documentation, you need to re-prepare the MediaPlayer (emphasis mine):
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.
Seems you are stopping the player because you are calling mediaPlayer.stop() this makes the MediaPlayer state to go in Stopped state. It will continue to play again when you call prepare() or prepareAsync() and has its preparation callback fired to start the playing media.

two media player played at once

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

Android: How to return back to previous activity when video is stopped playing?

I'm playing video from raw folder. Generally once its completed it remains in same activity. how to return back to previous activity automatically without pressing back button?
Are you using a VideoView to play the video in your 2nd activity? If so, you can use the OnCompletion event to call finish() on the activity, which will return you to the 1st activity.
Something like this should work:
VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
Log.i("VideoView", "onCompletion()");
finish();
}
});
Just be sure to replace R.id.videoView with the actual id in your layout file.
Hope that helps!
Use MediaPlayer.OnCompletionListener to Listen when Video Playing in finished in Activity 2 and start Previous activity on onCompletion as :
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
//start Previous Activity here
Current_Activity.this.finish();
}
}); // video finish listener
After playing a video onCompletion() method is invoked, in that just call finish() method in it.

android mediaplayer is playing multiple instances from one player

so im trying to make a sound play through android. ive created a mediaplayer like this
MediaPlayer player; (outside onCreate())
player = MediaPlayer.create(this, R.raw.theninth); (inside onCreate())
and then i reference this player throughout the Activity. .start() on shake. .pause onclick etc.. it looks like this
if(mag >= 500) {
if(!started) {
mainView.setText(R.string.waitforit);
player.start();
started = true;
if(doThisOnce) {
timer.schedule(changeText, 26000);
doThisOnce = false;
}
}
player.setVolume((float)(2*mag/3.0),(float)(2*mag/3.0));
}
and
public void stopAudio(View v) {
mainView.setText(R.string.shakeme);
player.pause();
started = false;
}
and so the problem is the player doesnt pause when i tap the screen, instead it goes to the starting text of hte app, and and its like i reloaded the entire app, and i can shake it and then another intance of the same file from the same player will start playing.. what do?

Categories

Resources