如何在媒体播放器中自动播放下一首歌?

时间:2017-09-10 09:46:54

标签: android android-mediaplayer

我看到这个问题在这里得到了很多回答。但我有一个问题。 当我的音乐播放器启动时,它会跳过第一首曲目并自动开始播放第二首曲目。

我希望它像普通的音乐付款人一样。

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);
}

}

如果已播放,我想播放下一首歌。打开我的应用程序后,我的音乐播放器开始播放第二首歌曲。

2 个答案:

答案 0 :(得分:0)

暂时不知道你打算在这里存档的内容,但你可以尝试只调用playSong();而不是同时调用它们。

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();
        }
    }
});
}

答案 1 :(得分:0)

void initMediaPlayer() {
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                try {

                    nextSong();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        try {
            playSong();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

请试着把它打得很好。