I have code that plays mp3 file from assets directory:
MediaPlayer mediaPlayer = new MediaPlayer();
descriptor = context.getAssets().openFd("beep.mp3");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setVolume(soundLevel, soundLevel);
descriptor.close();
mediaPlayer.setLooping(false);
mediaPlayer.prepare();
mediaPlayer.start();
This code was working fine on every device and every Android version. Until after Android L preview came available.
mediaPlayer.prepare();
throws IllegalStateException on Nexus 5 running Android L.
Emulator version of Android L works completely fine, I don't own Nexus 5 so can't say what exactly is causing the issue. I am thinking about descriptor.close()
line before prepare()
called, but not sure.
public void myBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("beep.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
};
来源:https://stackoverflow.com/questions/25189539/mediaplayer-prepare-throws-illegalstateexception-on-android-l