android - How to play next song automatically in media player? -
i have seen question answered lot of time here. have problem. when ever music player starts skips first track , and automatically starts playing 2nd track.
i want behave normal music payer.
public class musicservice extends service { private final mediaplayer mp = new mediaplayer(); private final ibinder localbinder = new localbinder(); arraylist<song> songs = new arraylist<>(); boolean firstattempt = true; public musicservice() { } @override public ibinder onbind(intent intent) { return localbinder; } public class localbinder extends binder{ musicservice getservice(){ return musicservice.this; } } @override public void oncreate() { super.oncreate(); setsongslist(); initmediaplayer(); } void initmediaplayer(){ mp.setaudiostreamtype(audiomanager.stream_music); mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mediaplayer) { try { if(firstattempt){ firstattempt = false; }else{ nextsong(); playsong(); } } catch (exception e) { e.printstacktrace(); } } }); } void setsongslist(){ string selection = mediastore.audio.media.is_music + " != 0"; string sortorder = mediastore.mediacolumns.display_name+""; string[] projection = { mediastore.audio.media.artist, mediastore.audio.media.title, mediastore.audio.media.data, mediastore.audio.media.duration }; cursor cursor = getapplicationcontext().getcontentresolver().query( mediastore.audio.media.external_content_uri, projection, selection, null, sortorder); while(cursor.movetonext()){ songs.add(new song(cursor.getstring(1), cursor.getstring(0), cursor.getstring(3), cursor.getstring(2))); } } song getplayingsong(){ return songs.get(nowplaying); } //--------song handlers start here-------- int nowplaying = 0; int seeklength = 0; void playsong(int index) throws exception { if(index != nowplaying){ seeklength = 0; } nowplaying = index; playsong(); } void playsong() throws exception { mp.reset(); uri path = uri.parse(songs.get(nowplaying).path); mp.setdatasource(string.valueof(path)); mp.prepare(); mp.seekto(seeklength); mp.start(); } void pausesong(){ mp.pause(); seeklength = mp.getcurrentposition(); } void nextsong() throws exception { nowplaying = nowplaying+1; if (nowplaying == songs.size()){ nowplaying = 0; } seeklength = 0; if(mp.isplaying()){ playsong(); } } void prevsong() throws exception { nowplaying = nowplaying-1; if(nowplaying < 0){ nowplaying = songs.size()-1; } seeklength = 0; if(mp.isplaying()){ playsong(); } } void queuesong(int index){ } //-------------returning mediaplayer details------------- boolean isplaying(){ return mp.isplaying(); } int getduration(){ return mp.getduration(); } int getcurrentposition(){ return mp.getcurrentposition(); } void seekto(int length){ seeklength = length; mp.seekto(length); } //------------returning song details---------- song getsongbyindex(int index){ return songs.get(index); }
}
i want play next song if playing. music player starts playing second song open application.
don't know intended archive here can try call playsong();
instead of calling both.
void initmediaplayer(){ mp.setaudiostreamtype(audiomanager.stream_music); mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mediaplayer) { try { playsong(); } catch (exception e) { e.printstacktrace(); } } }); }
Comments
Post a Comment