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.
Related
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
I am trying to edit player on the same layout as creating a player.
When i press the edit player button , I change the headings of the create player page to "Edit Player" and populate the listbox with the player name. I would like to know how i can return to the create player activity when i press done. Thankyou
Normally you would call finish() to finish the current activity, and the user should be redirected to the first activity on the stack, which apparently should be the Create Player activity.
So, set a click listener for the done button, an in onClick call finish()
Or, you may start the Create Player activity in onClick, like in you would start any regular activity:
Intent intent = new Intent(EditPlayer.this, CreatePlayer.class);
startActivity(intent);
When i press the edit player button , I change the headings of the
create player page to "Edit Player" and populate the listbox with the
player name.
Seems like you did all actions in one activity?
Better to create two activity if you want to switching. creating a player A activity -> edit pressed -> startActivity B -> editing -> done and finish() activity B -> resume A activity.
I have two activity (A, B):
A: Song Button
B(WebView): Song Play List - Embadded from URL
My question is, when user go to B to start play music and user back to A without stop the music (Music is playing in B)...but when user go to B again the WebView in B is new while the old B is still playing the music. So, how can allow user can go to old B to stop music?
I really stuck on this over a week :((
I do appreciate for your ur help.
You can use the following flag on the intent
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
while creating activity B. For more information about this, check the link below
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
I have an application with a list of video files with play button. When the user clicks play button, a separate activity starts through intent where the video is played. What I want is that when I click the back button when the video file finishes, I want the mainActivity to be refreshed (the mainActivity is the activity that started the activity for playing video file).
Any useful suggestions please??
Put your "refresh"-code in the Main-Activity's onStart-method.
See Androids Activity Lifecycle.
You could use startActivityForResult(Intent, int) and then handle the different results.