android - ANR Reason keyDispatchingTimedOut MediaPlayer -


in app, have listview of music items , when click item, fetch corresponding mp3 url stream in mediaplayer. set mediaplayer's datasource , more everytime user clicks on play button. problem when click play button, , scroll listview screen freezes dialog opens:

appname not responding. close it?       wait       ok 

but couldn't stacktrace said like:

anr my.package.name.appname ... reason: keydispatchingtimedout 

i've read place process inside background thread not make ui thread wait. current implementation problem still persists:

thread t = new thread() {             public void run() {                 sampleactivity.this.runonuithread(new runnable() {                     public void run() {                         try {                             mp.reset();                             mp.setdatasource(musicuri);                             mp.prepare();                             mp.start();                              songseekbar.setenabled(true);                              // changing button image stop image                             btnplay.setimageresource(r.drawable.btn_stop);                              // set progress bar values                             songseekbar.setprogress(0);                             songseekbar.setmax(100);                              // updating progress bar                             updateprogressbar();                         } catch (illegalargumentexception e) {                             e.printstacktrace();                         } catch (illegalstateexception e) {                             e.printstacktrace();                         } catch (ioexception e) {                             songseekbar.setenabled(false);                             songtotaldurationlabel.settext("0:00");                             songcurrentdurationlabel.settext("0:00");                             log.d(constant.tag_sample, musictitle                                     + " mp3 file not found.");                             e.printstacktrace();                         }                     }                 });             }         };         t.start(); 

what did place runonuithread inside thread (not working), tried thread (not working), , tried asynctask inside doinbackground player playing @ background , cannot stopped haven't tried placing inside onpostexecute

i have feeling i'm missing something. 1 thing's sure, should place inside background thread one? idea accepted, thank you!

edit: so, changed implementation still persists:

thread thread = new thread() {         public void run() {             handler refresh = new handler(looper.getmainlooper());             refresh.post(new runnable() {                 public void run() {                     // play song                     try {                         mp.reset();                         mp.setdatasource(globalsongindex);                         mp.prepare();                         mp.start();                          songseekbar.setenabled(true);                          // changing button image pause image                         btnplay.setimageresource(r.drawable.btn_stop);                          // set progress bar values                         songseekbar.setprogress(0);                         songseekbar.setmax(100);                          // updating progress bar                         updateprogressbar();                     } catch (illegalargumentexception e) {                         e.printstacktrace();                     } catch (illegalstateexception e) {                         e.printstacktrace();                     } catch (ioexception e) {                         songseekbar.setenabled(false);                         songtotaldurationlabel.settext("0:00");                         songcurrentdurationlabel.settext("0:00");                         log.d(constant.tag_mypage, musictitle + " mp3 file not found.");                         e.printstacktrace();                     }                 }             });         }     };     thread.start(); 

by way, based on android website here.

ok, code executes in main thread. it's not idea initialize player. totally didn't change first code second.

anr appears in string player.prepare(). try use playerasync instead of. prepare player asynchronous or remove this:

handler refresh = new handler(looper.getmainlooper());         refresh.post(new runnable() {             public void run() { 

and make job in background thread.

and read asynctask - class work in background. more useful using ordinary threads.

the right way looks it:

mediaplayer mp = new mediaplayer(); mp.setdatasource(youruri); mp.setonpreparedlistener(new onpreparedlistener() {   public void onprepared(mediaplayer mp)   {    long duration = mp.getduration();    mp.start();   } }); mp.prepareasync(); 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -