问题
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 :
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._stop(Native Method)
at android.media.MediaPlayer.stop(MediaPlayer.java:1561)
at com.ui.VideoDetailActivity.onPause(VideoDetailActivity.java:493)
at android.app.Activity.performPause(Activity.java:5686)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3400)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3369)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3347)
at android.app.ActivityThread.access$1100(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
VideoDetailActivity.java:493 contains:
mediaPlayer.stop(); inside onPause method ?
回答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());
}
来源:https://stackoverflow.com/questions/32644365/releasing-mediaplayer-and-stopping-it-onpause-and-onresume-gives-error-in-androi