必须从UI线程调用android方法setOnPreparedListener,当前推断的线程是worker

时间:2017-04-21 10:48:13

标签: android android-asynctask

我正在从异步任务加载视频并收到此错误

  

必须从UI线程调用方法setOnPreparedListener,当前推断的线程是worker。

 @Override
    protected Void doInBackground(Void... params) {

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
                videoView.start();
                duration = videoView.getDuration();
            }
        });

        do {

            current = videoView.getCurrentPosition();
            System.out.println("duration - " + duration + " current- "
                    + current);

            if (sync.isCancelled())
                break;

        }

        while (current != duration || current == 0);

        return null;
    }

怎么办?

1 个答案:

答案 0 :(得分:0)

将其移至onPreExecute(在UI线程上执行)。在AsyncTask内添加此内容:

    @Override
    protected void onPreExecute() {
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
                videoView.start();
                duration = videoView.getDuration();
            }
        });

    }