Handle Media Player - android

I have created an application that has 2 activities A and B.
From activity A I have a button to call B, in that B I have MediaPlayer to play a song list. When I press back button to back to A, then I press the button to call B again. All of my information's gone but the last Media Player keep playing, when I try to play another song, both Media play as the same. My problem is I want to keep the music playing when I press back button to A, and then I go back to B from A and play other songs without create new Media or just continue the last activity using the last Media Player.

check State diagram of the MediaPlayer. That should answer your problem. Read the javadoc about how and when to stop the player.

I would suggest you maintain a global MediaPlayer object in your Activity B.
public static final MediaPlayer player = new MediaPlayer():
In your activity B, whenever you make any change to the player just call
player.stop() or player.reset();
before starting the audio session.

Related

How to save mediaplayer instance on going to another fragment from current and again regaining it while returning back from that fragment?

I want to stop media player to what it is and go on that fragment guided by button in below and again while returning i want to get same paused media playerenter image description here
You can achieve that by following below approach
1) Create Media Player instance on Activity rather on Fragment
By this on whatever fragment you are you can get Media Player instance at any time without worrying the actual fragment
I want to stop media player to what it is and go on that fragment guided by button in below
2) On clicking stop button(as you mentioned, I am expecting this button on any fragment) get the instance of Media Player and call Media Player stop method.
and again while returning i want to get same paused media player
3) When you get back the same fragment, you will already have the same instance of Media Player. And then you can do you desire operation
I hope that helps you

How to stop music when clicking to new activity and be able to start it again from the beginning?

I have an audio playing in the background when I am clicking onto a new activity. I want while I am clicking to the next activity, for the audio, to stop.
However, when going back to the original activity I want the audio to start again from the beginning.
Using media player and Android Studio. Here's all the media player code I have so far:
MediaPlayer mediaPlayer = MediaPlayer.create(pagetwo.this,R.raw.audio1);
mediaPlayer.start();
You can use the basic lifecycle methods of your Activity to handle this use case:
Android will call onPause() if you open a new Activity and will call onResume() if you enter/come back to the first activity.
If you call mediaPlayer.start() in onResume() and mediaPlayer.stop() in onPause() you should have the desired behavior.

Play MediaPlayer In Background

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.

screen lock or pressing Home Button pause the media player,then playing it after Resume

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.

Android: Switch between Activities

I am trying to develop an audio player.
My first activity has a list of the mp3's from my sd card and I can choose some them and put them in an ArrayList. I have a button (Add to playlist), and when I press it, it starts the new activity where I take the ArrayList, there I create my player, play music etc. But... when I press the back button on the device(you know this that look like such as a half-circled arrow), I go back to my first activity, but the music don't stop and I can choose again songs and if I press the Add To Playlist button then the app plays two songs simultaneously (my first and this second).
How can I handle this? (I want when i go back, choose or unchoose some tracks and then when i press the button (Add to playlist) to play my new list).
I start my second activity like that:
Intent intent = new Intent (Chooser.this, Player.class);
Bundle b = new Bundle();
b.putStringArrayList("key", plist);
b.putStringArrayList("pos", po);
intent.putExtras(b);
startActivity(intent);
I don't want my player stops, simply I want to add or remove songs in my playlist.
You want to implement the one of the Android Lifecycle methods in your second Activity and tell it to stop the MediaPlayer.
The ones you may be interested in are onPause(), onStop(), and onDestroy().
You can use a global variable to maintain the "is playing" state. If you find the user tries to play a new song and the isPlaying variable is true then stop the player before starting the new song.

Categories

Resources