4

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.

nemezis
  • 536
  • 1
  • 6
  • 21
  • 1
    I'm seeing this same behavior on Android L Preview. Not sure how to fix. – Chase Florell Oct 02 '14 at 22:49
  • Facing the same problem with nexus 5 did you find a solution for that problem or a bypass? – Eran Katsav Nov 14 '14 at 08:06
  • @EranKatsav no, I didn't find a solution. Is this issue happening on the release version of Android 5.0 for you? – nemezis Nov 14 '14 at 08:09
  • Yes, they released this morning.. This is critical for my app. Did it only happened with nexus 5 running L? did you check with other devices running L? – Eran Katsav Nov 14 '14 at 08:11
  • No, I don't think there are any devices (except maybe Nexus 7) that running Android L now. I don't have nexus 5, so I can't check it. – nemezis Nov 14 '14 at 08:13
  • We will try to think of a solution and fast. users who will get the update are going to have a problem. – Eran Katsav Nov 14 '14 at 08:16
  • If you do find a solution - please post it here as an answer. – nemezis Nov 14 '14 at 08:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64931/discussion-between-nemezis-and-eran-katsav). – nemezis Nov 14 '14 at 10:50

1 Answers1

0
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();
    }
};
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • Thanks for this notice, but thing is that `IllegalStateException` is thrown when `prepare()` called; – nemezis Aug 07 '14 at 18:32