一些用户在我的应用程序在线 threadAudio.start() 上遇到 (ANR keyDispatchingTimedOut) 错误
private void startAudio(int audioId) {
mPlayer = MediaPlayer.create(this, audioId);
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
mPlayer = null;
}
});
mPlayer.start();
vSeek.setMax(mPlayer.getDuration());
threadAudio = new Thread(thread);
threadAudio.start();
}
public Runnable thread = new Runnable() {
public void run() {
try {
while(mPlayer != null){
int currentPosition = mPlayer.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
handler.sendMessage(msg);
}
Thread.sleep(100);
} catch (Exception e) {
}
}
};
private Handler handler = new Handler() {
public void handleMessage(Message msg){
vSeek.setProgress(msg.what);
}
};
首先,我无法在我的设备上重现错误!这可以是特定于设备的吗?
我正在按照 android doc 建议使用处理程序,以避免从 UI 线程外部访问 Android UI 工具包...
或者也许我必须在两行之间的某个地方调用 threadAudio.stop() ?