MediaPlayer play audio in background and play next one

≯℡__Kan透↙ 提交于 2019-12-01 06:22:24

问题


I use below code to play audio in background:

String[] Path = new String[] {path1, path2, ...};
mMediaPlayer.setDataSource(Path[i]);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.seekTo(0);

While I play the first one Path[0] in background. I want to make it auto play next one Path[1] after Path[0] play finish, how to arrive it?


回答1:


You should override onCompletionListener like this,

mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {           
    public void onCompletion(MediaPlayer mp) {          
        Log.i("Completion Listener","Song Complete");
        mp.stop();
        mp.reset();
        mp.setDataSource([nextElement]);
        mp.prepare();
        mp.start();
    }
});

If you use a onPreparedListener in your MediaPlayer then you cal also use the prepareAsync command and ignore the .start().




回答2:


You need to set an OnCompletionListener to your MediaPlayer, in the listener set the source to path2, prepare and play. http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html



来源:https://stackoverflow.com/questions/10223745/mediaplayer-play-audio-in-background-and-play-next-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!