Lemans Trương
Lemans Trương

Reputation: 1

Handle Media Player

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.

Upvotes: 0

Views: 105

Answers (2)

Dexter
Dexter

Reputation: 1750

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.

Upvotes: 0

Nazgul
Nazgul

Reputation: 1902

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

Upvotes: 1

Related Questions