@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;
}
}
It works, but after song ends I have this error message in the log:
03-13 12:52:05.101: ERROR/MP3Extractor(34): Unable to resync. Signalling end of stream. 03-13 13:16:59.585: INFO/StagefrightPlayer(34): setDataSource('/mnt/sdcard/Audio/sound.mp3')
You should really not just have empty catch blocks, you may be getting an explanation to that error but your just ignoring it try this:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With