Some users are getting (ANR keyDispatchingTimedOut) error with my app on line threadAudio.start()
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);
}
};
First, i am not able to reproduce the error on my device! Can this be device-specific?
I am using handler as per android doc suggestion to avoid accessing the Android UI toolkit from outside the UI thread…
Or maybe I have to call threadAudio.stop() somewhere between the lines?
I think you meant to put
Thread.sleep(100);inside yourwhile(mPlayer != null){loop? Essentially your while loop is continuously sending armies upon armies of message to your handler, which runs on the UIThread, and this is what caused ANR keyDispatchingTimedOut error: all the CPU time are either spent processing those messages OR spent on sending those messages. Not a good place to be in 😛