我有一个带有音乐播放器的应用程序,我正在使用@override方法OnCompletionListener来听音乐完成并传递给下一首歌曲,波纹管代码可以正常工作,但是在第二首歌曲中完成OncompletionListener时没有调用再次和音乐在完成时停止,如何不断传递给下一首歌?
OnCompletionListener
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
NextSong();
}
});
NextSong 方法
public void NextSong (){
num = posicion++;
if (MainActivity.num <= MainActivity.listaArray.size() - 2) {
mediaPlayer.reset();
uri = Uri.parse(String.valueOf(archivosMp3[num + 1]));
mmr.setDataSource(String.valueOf(archivosMp3[num + 1]));
mediaPlayer = mediaPlayer.create(Servicio_Reproductor.this, MainActivity.uri);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
答案 0 :(得分:1)
试试这个,
public class MediaplayerActivity extends Activity implements OnCompletionListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
mMediaPlayer.setOnCompletionListener(this);
}
@Override
public void onCompletion(MediaPlayer mp) {
.
.
.
mediaPlayer = mediaPlayer.create(Servicio_Reproductor.this, MainActivity.uri);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}
});
}
}