检测音乐播放完毕
Detect music finished playing
我有通过
播放的音乐
AssetFileDescriptor assetDescriptor = getAssets().openFd(filename);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mediaPlayer.prepareAsync();
mediaPlayer.setVolume(1, 1);
mediaPlayer.start();
我应该如何检测此音乐是否停止播放以便我可以开始播放下一首?
我尝试通过
不断检查(每秒一次)
((AudioManager) application.getSystemService(Context.AUDIO_SERVICE)).isMusicActive();
但这似乎不起作用(总是 returns 正确)。有没有其他方法可以让我收到音乐停止播放的通知,以便我可以开始下一首?
MediaPlayer
有一个名为 setOnCompletionListener
的方法,它执行以下操作:
Register a callback to be invoked when the end of a media source has been reached during playback.
使用MediaPlayer.onCompletionListener
监听曲目结束时的事件。此后将播放下一个音频文件。一个例子
player.prepare();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.stop();
// next audio file
}
});
player.start();
一个额外的建议,一个接一个地播放多个音频,你不必创建嵌套的onCompletionListener。请改用以下代码。
int[] myAudio = {R.raw.audio1, R.raw.audio2, R.raw.audio3};
int mTrackFinish = 0;
MediaPlayer mp = MediaPlayer.create(this, myAudio[0]);
mp.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
mTrackFinish++;
mp.reset();
if(mTrackFinish < myAudio.length)
// play some audio from list
else
mTrackFinish=0;
mp.release();
mp = null; // either set counter to 0 and start again or call end of list
}
});
mp.start();
我有通过
播放的音乐AssetFileDescriptor assetDescriptor = getAssets().openFd(filename);
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mediaPlayer.prepareAsync();
mediaPlayer.setVolume(1, 1);
mediaPlayer.start();
我应该如何检测此音乐是否停止播放以便我可以开始播放下一首?
我尝试通过
不断检查(每秒一次)((AudioManager) application.getSystemService(Context.AUDIO_SERVICE)).isMusicActive();
但这似乎不起作用(总是 returns 正确)。有没有其他方法可以让我收到音乐停止播放的通知,以便我可以开始下一首?
MediaPlayer
有一个名为 setOnCompletionListener
的方法,它执行以下操作:
Register a callback to be invoked when the end of a media source has been reached during playback.
使用MediaPlayer.onCompletionListener
监听曲目结束时的事件。此后将播放下一个音频文件。一个例子
player.prepare();
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
player.stop();
// next audio file
}
});
player.start();
一个额外的建议,一个接一个地播放多个音频,你不必创建嵌套的onCompletionListener。请改用以下代码。
int[] myAudio = {R.raw.audio1, R.raw.audio2, R.raw.audio3};
int mTrackFinish = 0;
MediaPlayer mp = MediaPlayer.create(this, myAudio[0]);
mp.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
mTrackFinish++;
mp.reset();
if(mTrackFinish < myAudio.length)
// play some audio from list
else
mTrackFinish=0;
mp.release();
mp = null; // either set counter to 0 and start again or call end of list
}
});
mp.start();