MediaPlayer的reset()方法的作用

MediaPlayer的reset()方法经常使用,但具体为什么要用却不是很明白,今天看MediaPlayer的API说明,才知道是为了重用MediaPlayer对象。

在MediaPlayer的API说明中有如下内容:

In order to reuse a MediaPlayer object that is in the Error state and recover from the error, reset() can be called to restore the object to its Idle state.

也就是说,为了重用处于Error错误状态的MediaPlayer对象,可以通过调用reset()方法,使其恢复到idle空闲状态。

http://developer.android.com/reference/android/media/MediaPlayer.html



发布了270 篇原创文章 · 获赞 276 · 访问量 379万+
展开阅读全文

结束activity中出现MediaPlayer异常

02-22

在结束mediaplayer activity时出现异常,代码: 结束按钮: ImageView imageAllBack = (ImageView)this.findViewById(R.id.imageAllBack); imageAllBack.setOnClickListener(new OnClickListener() { public void onClick(View v) { //mediaPlayer.stop(); if(mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } finish(); } }); mediaPlayer事件: @Override protected void onStop() { super.onStop(); mediaPlayer.stop(); mediaPlayer.release(); } @Override public boolean onTouchEvent(MotionEvent event) { mediaController.show(); return false; } //--MediaPlayerControl methods---------------------------------------------------- public void start() { mediaPlayer.start(); } public void pause() { mediaPlayer.pause(); } public int getDuration() { return mediaPlayer.getDuration(); } public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mediaPlayer.seekTo(i); } public boolean isPlaying() { return mediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; } //-------------------------------------------------------------------------------- public void onPrepared(MediaPlayer mediaPlayer) { mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } public class MyMediaController extends MediaController { public MyMediaController(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void hide() { // Do Nothing to keep the show the controller all times } } 异常: 02-21 21:03:33.829: E/AndroidRuntime(8889): FATAL EXCEPTION: main 02-21 21:03:33.829: E/AndroidRuntime(8889): java.lang.IllegalStateException 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.media.MediaPlayer.isPlaying(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.isPlaying(NaturaPlayer.java:126) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.getCurrentPosition(NaturaPlayer.java:115) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.setProgress(MediaController.java:436) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.access$500(MediaController.java:74) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController$3.handleMessage(MediaController.java:407) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Handler.dispatchMessage(Handler.java:99) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Looper.loop(Looper.java:137) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.app.ActivityThread.main(ActivityThread.java:4441) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-21 21:03:33.829: E/AndroidRuntime(8889): at dalvik.system.NativeStart.main(Native Method) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览