Step #1: Implement a Service
.
Step #2: Have the activity send commands to the service in the form of Intents
delivered via startService()
(except for "stop", which would be a call to stopService())
.
Step #3: Have the service handle those commands in onStartCommand()
by making appropriate calls on MediaPlayer
(be sure to use prepareAsync()
).
Here is a trivial little example of this, for play and stop, with stubbed-out actual media usage. Here is a slightly more sophisticated example of this, using a Notification
and startForeground()
to ensure the player can keep playing.