android - Stop or release MediaPlayer while it is still preparing -
is bug or not possible release, stop or kill mediaplayer
while it's preparing?
i have instance of mediaplayer
running in service
, stops fine if stop, release, set null, while mediaplayer
in prepared state. doesn't if stop, release, set null if it's in preparing state.
onprepared()
called after stop, release, setting null. workaround this?
i think it's common use case when user wants stop mediaplayer
before has finished preparing.
by looking @ mediaplayer
documentation, you're not allowed call stop()
on uninitialized object; makes sense because can't stop not running/ready yet.
on other hand, release()
seems trick after looking @ the source code of mediaplayer
.
but doesn't harm add boolean flag indicate there no need mediaplayer
object anymore , use flag release object if onprepared()
gets called.
a pseudocode this:
public void cancel(){ mcancel = true; } public void onprepared(mediaplayer player){ if(mcancel){ player.release(); //nullify mediaplayer reference mediaplayer = null } }
Comments
Post a Comment