MediaPlayer.prepare() throws IllegalStateException on Android L

有些话、适合烂在心里 提交于 2020-01-02 05:12:14

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!