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.
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 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.
I am developing media player application in which i am showing a notification when song starts playing in a service.
Basically i am showing a list view in my first activity(being any one from Artist tab, album tab etc) and on click of an item , player screen is shown.
I have to implement a back button in my player activity, i guess i just have to do a finish() on click of it.
But in case my previous activity is destroyed and i am coming through pending intent(through notification), if i call finish() now the activity is simply destroyed. In such case i want to recreate the previous activity.
Example :
Album > player screen is shown and songs starts in service with status bar notification..
Suppose i press back now and destroy all the activity.
Click on notification > Player is shown again.. if i press back now Album activity should be shown similarly for artist tab.
So far i am just doing:
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
I guess you can achieve what you need with setting the intent flags accordingly. The best introduction to these flags I found is the following
Android Activities and Tasks series – Intent flags
It is still a confusing subject for me yet the Android application offered along with this article is very helpful to check quickly what the different flags do.
I thinking you must set more informations about song in Service like a album and when you press back in song activity you should chek service and give back it from Service and init album activity
How stop playing audio when click back button ? or exit application it's keeping playing on background
I made application has 2 activities first Main welcome activity you click on start button show second activity which contain grid view for multiple images when click on an image start play different sound , but when click back button switch to Main activity but sound keep playing !! and if click home also keep playing?
how to stop it ?
Tell it to stop in the onPause() of the Activity.
#Override
public void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
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.