I have an activity that starts and binds to a music playing service, hence I have implemented both the onStartCommand
and onBind
methods. I have a MediaPlayer
object in the service used to play media files.
These are the steps I perform:
MediaPlayer
Object with a valid
mediaLocation and the media starts playingunbindService(mServiceConn)
method to unbind to the service, the
media keeps playingstopSelf
method, and remove the notification
using the notificationManager.cancel(ID)
service
was both started
and bound
I
have called both the unbind
and stopSelf
methods, so acc. to the android docs
this would be enough to stop a started-bound service.service
's onDestroy
also gets called(checked using
Logcat outputs)MediaPlayer
keeps on playing the musicMy confusion is, if the UI(activity) and notification both are destroyed, then shouldn't the whole process be destroyed, hence making the MediaPlayer
stop? Or is the service
not destroyed, which is why the media is still being played?
I could work around the media being played by forceFully stopping it in the service
's onDestroy
or similar methods but how do I even know the service (and the process in it's entirety) is all cleaned up? So, it's not taking up any unseen resources?
PS.: The code was too long to post a runnable example, so no code was posted, yet mostly it's just standard stuff, nothing from personal logic was added.
When you use media player
to play music, the media player does not directly play the music, but requests the playback engine of the android system to play the music.
When you no longer use the playback engine of the android system, you should call the release()
method of the media player
to release system resources.