prepare()在Android L上抛出IllegalStateException

时间:2022-10-19 18:51:31

I have code that plays mp3 file from assets directory:

我有从资产目录播放mp3文件的代码:

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.

这段代码在每个设备和Android版本上运行良好。直到Android L预览之后。mediaPlayer.prepare();在Nexus 5运行Android L时抛出IllegalStateException。

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.

Android L的仿真版完全没问题,我没有Nexus 5,所以说不出到底是什么引起了这个问题。我正在考虑描述符.close()行,然后准备()调用,但不确定。

1 个解决方案

#1


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();
    }
};

#1


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();
    }
};