@Override
public void onCreate(Bundle savedInstanceState) {
...
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
playAudio(getNextAudioFile());
}
});
..
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
private boolean playAudio(String file) {
if(file.isEmpty()) {
return true;
}
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
mMediaPlayer.reset();
mAudioFile = new File(mBaseAudioDir, file);
if(mAudioFile.canRead()) {
try {
mMediaPlayer.setDataSource(mAudioFile.getPath());
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
return false;
} catch (IllegalStateException e) {
return false;
} catch (IOException e) {
return false;
}
mMediaPlayer.start();
return true;
} else {
return false;
}
}
它可以工作,但是在歌曲结束后,我在日志中出现了这条错误消息:
03-13 12:52:05.101: 错误/ MP3Extractor(34):无法 重新同步。信号流结束。 03-13 13:16:59.585: INFO / StagefrightPlayer(34): 的setDataSource( '的/ mnt / SD卡/音频/ sound.mp3')
答案 0 :(得分:2)
你真的不应该只有空的拦截块, 你可能会得到一个解释错误,但你只是忽略它 试试这个:
try {
mMediaPlayer.setDataSource(mAudioFile.getPath());
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
Log.e(TAG, "mMediaPlayer IllegalArgumentException " ,e);
return false;
} catch (IOException e) {
Log.e(TAG, "mMediaPlayer IOException" ,e);
return false;
}