Android Playing MediaPlayer in SeparateThread -
i have mp3link need play on device in separate thread
i have tried this,but when execute code ,i'm not able play music,could 1 @ code,what went wrong?
here code:
thread trd = new thread(new runnable(){ public void run(){ //code http request mediaplayer mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); try { mediaplayer.setdatasource(mp3link); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (securityexception e) { e.printstacktrace(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } mediaplayer.prepareasync(); // can show progress dialog here untill prepared play mediaplayer.setonpreparedlistener(new onpreparedlistener() { public void onprepared(mediaplayer mp) { // dismis progress dialog, media palyer start playing log.d("mediaplyer>>>>>>>>", "mediaplyer>>>>>>>>"); mp.start(); } }); mediaplayer.setonerrorlistener(new onerrorlistener() { public boolean onerror(mediaplayer mp, int what, int extra) { // dissmiss progress bar here. come here when // mediaplayer // not able play file. can show error message user return false; } }); } }); trd.start();
how put
mediaplayer.prepareasync();
at end of run()
function ?
thread trd = new thread(new runnable(){ public void run(){ //code http request mediaplayer mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); try { mediaplayer.setdatasource(mp3link); } catch (illegalargumentexception e) { e.printstacktrace(); } catch (securityexception e) { e.printstacktrace(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } // mediaplayer.prepareasync(); // <== marked // can show progress dialog here untill prepared play mediaplayer.setonpreparedlistener(new onpreparedlistener() { public void onprepared(mediaplayer mp) { // dismis progress dialog, media palyer start playing log.d("mediaplyer>>>>>>>>", "mediaplyer>>>>>>>>"); mp.start(); } }); mediaplayer.setonerrorlistener(new onerrorlistener() { public boolean onerror(mediaplayer mp, int what, int extra) { // dissmiss progress bar here. come here when // mediaplayer // not able play file. can show error message user return false; } }); mediaplayer.prepareasync(); // <== add } }); trd.start();
hope helps.
Comments
Post a Comment