android ANR in MediaPlayer reset

你。 提交于 2019-12-03 22:28:54

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

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

One technique that prevents the Android system from concluding a code that has been responsive for a long period of time is to create a child thread. Within the child thread, most of the actual workings of the codes can be placed, so that the main thread runs with minimal periods of unresponsive times.

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!