在MediaPlayer的复位安卓ANR [英] android ANR in MediaPlayer reset
问题描述
我有它通过VideoView播放视频的一个简单的活动
I've got a simple activity which plays video through VideoView
public class AVideo extends Activity {
private VideoView mVideoView;
private MediaController mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video);
Bundle extras = getIntent().getExtras();
Uri path = Uri.parse(extras.getString("videoURI"));
mVideoView = (VideoView) findViewById(R.id.video);
mVideoView.setVideoURI(path);
mc = new MediaController(this);
mVideoView.setMediaController(mc);
}
@Override
protected void onResume() {
super.onResume();
mVideoView.start();
mc.show();
}
}
在当用户按下后退按钮,在这个活动有些情况下,视频开始播放之前,他得到了ANR。 这是我的痕迹:
In some cases when user pushes back button, on this activity, before video starts playing, he's got ANR. Here's my traces:
DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
| group="main" sCount=1 dsCount=0 s=N obj=0x4001b268 self=0xbd00
| sysTid=423 nice=0 sched=0/0 cgrp=default handle=-1344001384
at android.media.MediaPlayer._reset(Native Method)
at android.media.MediaPlayer.reset(MediaPlayer.java:1028)
at android.widget.VideoView.release(VideoView.java:476)
at android.widget.VideoView.access$2100(VideoView.java:49)
at android.widget.VideoView$6.surfaceDestroyed(VideoView.java:467)
at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:488)
at android.view.SurfaceView.updateWindow(SurfaceView.java:413)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:189)
at android.view.View.dispatchWindowVisibilityChanged(View.java:3782)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
at android.view.ViewRoot.performTraversals(ViewRoot.java:706)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
另外的logcat表明这一点:
Also logcat shows this:
07-01 15:11:40.408: WARN/libutils.threads(2967): Thread (this=0x22818): don't call
waitForExit() from this Thread object's thread. It's a guaranteed deadlock!
我觉得<一个href="http://stackoverflow.com/questions/4742589/android-mediaplayer-bug-when-using-reset-and-release-calls">this问题和这个bug ,但目前还没有解决方案。
I find this question and this bug, but there are no solution.
推荐答案
如果您收到了ANR消息,意味着你的applicaction是不足以应对一段时间,使用 <一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29"相对=nofollow> runOnUiThread 运行在UI线程上指定的操作或的 的AsyncTask
If you are getting the ANR message, means that your applicaction is insufficiently responsive for a period of time, use runOnUiThread to run the specified action on the UI thread or Asynctask
public class AVideo extends Activity {
private VideoView mVideoView;
private MediaController mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video);
Bundle extras = getIntent().getExtras();
Uri path = Uri.parse(extras.getString("videoURI"));
mVideoView = (VideoView) findViewById(R.id.video);
runOnUiThread(new Runnable() {
@Override
public void run() {
mVideoView.setVideoURI(path);
}
});
mc = new MediaController(this);
mVideoView.setMediaController(mc);
}
@Override
protected void onResume() {
super.onResume();
mVideoView.start();
mc.show();
}
}
尝试实施挂线preparedListener开始播放视频。
Try implementing setOnPreparedListener to start the video.
mVideoView.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("VIDEO", "onCompletion");
}
});
mVideoView.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("VIDEO", "onPrepared");
if(mVideoView.canSeekForward()) mVideoView.seekTo(mVideoView.getDuration()/5);
mVideoView.start();
}
});
mVideoView.setKeepScreenOn(true);
这篇关于在MediaPlayer的复位安卓ANR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!