MediaPlayer中的android ANR重置有什么方法?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (183)

我有一个简单的活动,通过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。这是我的痕迹:

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显示了这一点:

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!

提问于
用户回答回答于

阻止Android系统结束长时间响应的代码的一种技术是创建一个子线程。在子线程中,可以放置代码的大部分实际工作,以便主线程以最少的无响应时间运行。

用户回答回答于

如果你收到ANR消息,意味着你的应用程序在一段时间内回应不足,请使用runOnUiThread在UI线程或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();
    }
  }

尝试实施setOnPreparedListener以启动视频。

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

扫码关注云+社区