释放Mediaplayer并停止onPause和onResume会在Android中产生错误

编程入门 行业动态 更新时间:2024-10-19 15:43:42
本文介绍了释放Mediaplayer并停止onPause和onResume会在Android中产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用videoView和mediaplayer但在onPause和onResume中停止mediaplayer会给我错误:

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(); } }); }

一旦我按下backArrowImageView按钮,我就在我的应用上方编写了代码崩溃,并给了我这个日志记录:

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 ?

推荐答案

根据Android文档:

According to the Android documentation:

"IllegalStateException,如果内部播放器引擎尚未建立 已初始化或已被释放."

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

因此,请确保已初始化MediaPlayer,并且不要使用已发布的MediaPlayer. 在onPause()和onDestroy()中使用以下方式,

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){ }

更多推荐

释放Mediaplayer并停止onPause和onResume会在Android中产生错误

本文发布于:2023-11-02 17:13:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   错误   onPause   Mediaplayer   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!