Releasing mediaplayer and stopping it onPause and onResume gives error in Android

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

i m using videoView and mediaplayer but stopping mediaplayer in onPause and onResume gives me error:

 static MediaPlayer mediaPlayer;  private VideoViewCustom videoView;  @Override     public void onCreate(final Bundle savedInstanceState) {          super.onCreate(savedInstanceState);         setContentView(R.layout.activity_detailvideo);         context = this;         videoView = (VideoViewCustom) findViewById(R.id.videoplayer);         //initialize media player         mediaPlayer = new MediaPlayer();         videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {             // Close the progress bar and play the video             public void onPrepared(MediaPlayer mp) {                 Log.d(TAG,"setOnPreparedListener");                 mediaPlayer = mp;                 int timeDuration = 0;                 boolean videoPlaying = false;                 if(savedInstanceState != null) {                     Log.d(TAG,"savedInstanceState != null");                     timeDuration = savedInstanceState.getInt("timeduration");                     videoPlaying = savedInstanceState.getBoolean("videoPlaying");                     Log.d(TAG, "timeDuration saved:" + timeDuration);                     Log.d(TAG, "videoPlaying saved:" + videoPlaying);                     Log.d(TAG,"video position:"+videoView.getCurrentPosition());                     if (videoPlaying) {                         videoView.seekTo(timeDuration);                         mediaPlayer.start();                         videoView.start();                     } else {                         videoView.seekTo(timeDuration);                     }                 }                 else                 {                     Log.d(TAG, "savedInstanceState == null");                     mediaPlayer.start();                     videoView.start();                 }                  finalTime = videoView.getDuration();                 Log.d(TAG, "mp.getCurrentPosition():" + videoView.getCurrentPosition());                 Date d = new Date(videoView.getCurrentPosition());                 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");                 String time = sdf.format(d).toString();                 Log.d(TAG, "time:" + time);                 videoSeekBar.setProgress(timeDuration);                 videoSeekBar.setMax(finalTime);                durationHandler.postDelayed(updateSeekBarTime,1000);             }         }); }  @Override     public void onResume() {         super.onResume();         // Setup the player         videoView.resume();     }     @Override     public void onPause()     {         if(mediaPlayer != null)         {             mediaPlayer.stop();         }         super.onPause();      } @Override     public void onDestroy() {         if(mediaPlayer != null) {             mediaPlayer.release();         }         super.onDestroy();     }  backArrowImageView.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if(mediaPlayer != null)                  {                     mediaPlayer.stop();                     mediaPlayer.release();                 }                 finish();             }         }); } 

once i press backArrowImageView button i have written code above my app crashed and giving me this logcat :

回答1:

According to the Android documentation:

"IllegalStateException if the internal player engine has not been initialized or has been released."

So ensure your MediaPlayer is initialized, and you don't use the released one. Use like below in onPause() and onDestroy(),

try{     if(mediaPlayer !=null && mediaPlayer.isPlaying()){        Log.d("TAG------->", "player is running");        mediaPlayer.stop();        Log.d("Tag------->", "player is stopped");        mediaPlayer.release();        Log.d("TAG------->", "player is released");     } }catch(Exception e){ } 


回答2:

Try Using this...u need to check for null and u cant Stop a Non Playing Media so check for Playing too before you release or Stop

    @Override     public void onPause()     {         if(mediaPlayer != null && mediaPlayer.isPlaying())         {             mediaPlayer.stop();         }         super.onPause();     } 


回答3:

According to official documentation you MUST avoid calling stop() in IDLE, INITIALIZED or ERROR states of MediaPlayer.

release() and reset() API can be called in any state of MediaPlayer.

Unfortunately to find out current state you have to track it yourself (there is no API for that). In one of my project I create class which extend MediaPlayer and by overriding some of MediaPlayer API track it's current state, so I can easily check current state before invoking any API. If you invoke API in incorrect state usually state changes to ERROR and you have to re-initialize media player again to fix it.



回答4:

before realeasing mediaplayer . use this line of code

mVideoView.setVisibility(View.GONE); 

Here an example .

try{     video.setVisibility(View.GONE);     mp.reset();     mp.release();     mp = null;                            }catch(Exception e){     Log.d("Releasing mp", e.toString());     } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!