stop mediaplayer from previous activity

by MarcC » Wed, 17 Feb 2010 02:21:55 GMT


Sponsored Links
 Can anyone tell me how to stop media player that was started in a
previous activity?

For ex. In activity-1 button pressed that did - mp.start(); then
activity-2 starts for different layout with new buttons, then when
button pressed, mp.stop is called, but does not stop the media from
the first activity.

Thanks in advance.

--



stop mediaplayer from previous activity

by Mark Murphy » Wed, 17 Feb 2010 02:28:05 GMT


 


You can't directly, AFAIK.


You would need the second activity to somehow notify the first activity
to stop the playback. Ideally, you would just do all of this in one
activity, or have the MediaPlayer be managed by a service, as
activity<->activity communications on-the-fly strikes me as a more
fragile pattern.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

--


Sponsored Links


stop mediaplayer from previous activity

by MarcC » Wed, 17 Feb 2010 02:39:01 GMT


 Thank you Mark....

Do you happen to know where I can find an example of MediaPlayer as a
Service? Did some searching but what I found didn't get me to where I
wanted to be. The examples I found had a single audio being played and
stopped via the service and I could not figure out how to add more
than one audio file.

-Marc






--



stop mediaplayer from previous activity

by Mark Murphy » Wed, 17 Feb 2010 02:41:33 GMT


 


I suspect the built in Music app works that way. In general, though,
MediaPlayer isn't activity-specific, so it should work in a service just
as it would in an activity. You might want to use the new
startForeground() method, though, to keep it playing and put a
Notification in the status bar at the same time.


Register a listener with the MediaPlayer and kick off the next file. If
the gap becomes a problem, consider using two MediaPlayers, working
back-to-back -- when #1 stops, you start #2 while doing prepareAsuync()
on #1 to ready it for the next go-around.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

_Beginning Android_ from Apress Now Available!

--