java - mediaPlayer CompletionListener playing next song before Completion of song -


i have written mediaplayer application , in mediaplayer.setoncompletionlistener called before completion of song , goes on every song...it's keep on going next song until reaches last song in playlist... please help

here code:

 trackslistviewforspeakers.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> adapterview, view view, int position, long l) {                 track track = playlistcontents.get(position);                 string filepath = track.getfilepath();                 if (mediaplayer.isplaying()) {                     mediaplayer.stop();                 }                 mediaplayer.reset();                 try {                     mediaplayer.setdatasource(filepath);                     mediaplayer.start();  //                    mediaplayer.setvolume(0, 0);                 } catch (ioexception e) {                     e.printstacktrace();                     for(int i=0;i<4;i++)                         system.out.println("mediaplayer exception");                 }              }         }); 

completion listener

mediaplayer.setoncompletionlistener(new mediaplayer.oncompletionlistener() {             @override             public void oncompletion(mediaplayer mp) {                 onnextclicked(mp);             }         }); 

onnextclickedmethod

public void onnextclicked(mediaplayer mp){ int val = mymodel.getcurrentplayingindex(); val=val+1; track track = currentplaylist.get(val);         string filepath = track.getfilepath(); if (mp.isplaying()) {             mp.stop();         }         mp.reset();         try {             mp.setdatasource(filepath);             mp.start(); //            mediaplayer.setvolume(0, 0);         } catch (ioexception e) {             e.printstacktrace();         }    }  } 

try this

//set path mediaplayer.setdatasource("yourfilepath"); mediaplayer.setonpreparelistener(new onpreparelistener(mediaplayer mediaplayer) {        mediaplayer.start(); }); mediaplayer.prepareasync(); 

don't call start() without prepareasync(); hope solve problem..


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -