我正在从异步任务加载视频并收到此错误
必须从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;
}
怎么办?
答案 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();
}
});
}