Stop or release MediaPlayer while it is still preparing

后端 未结 2 1376
日久生厌
日久生厌 2020-12-10 05:54

Is it a bug or is it not possible to release, stop or kill MediaPlayer while it\'s preparing?

I have an instance of MediaPlayer running in

相关标签:
2条回答
  • 2020-12-10 06:29

    By looking at the MediaPlayer documentation, you're not allowed to call stop() on an uninitialized object; which makes sense because you can't stop what is not running/ready yet.

    On the other hand, release() seems to do the trick after looking at the source code of MediaPlayer.

    But it doesn't harm to add a boolean flag to indicate that there is no need for the MediaPlayer object anymore and use that flag to release your object if onPrepared() gets called.

    A pseudocode would look like this:

    public void cancel(){
     mCancel = true;
    }
    
    public void onPrepared(MediaPlayer player){
      if(mCancel){
       player.release();
       //nullify your MediaPlayer reference
       mediaPlayer = null
      }
    }
    
    0 讨论(0)
  • 2020-12-10 06:31

    Another solution could be to implement the OnBufferingUpdateListener and override its method like this:

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        if (cancel) {
            mp.release();
            mMediaPlayer = null;
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题