出现错误 E/MediaPlayer:尝试停止并再次播放声音时出现错误 (1, -19)

Getting error E/MediaPlayer: error (1, -19) when trying to stop and play sound again

我有多个按钮,点击它们时我想要声音。我的代码是这样的

button.setOnClickListener(new Button.OnClickListener() {
          @Override
          public void onClick(View view) {                  
            if(mSound != null && mSound.isPlaying()){
                mSound.stop();
                mSound.reset();
                mSound.release();
                mSound = null;
            }
            mSound = new MediaPlayer();
            mSound = MediaPlayer.create(getApplicationContext(), R.raw.button);
            mSound.start();
            }
        });

在OnCreate中,我是这样初始化mSound的,

mSound = new MediaPlayer();
mSound = MediaPlayer.create(this, R.raw.button);

我收到错误 (1,-19) 以及 (0,38)。

注意:这不是重复的问题。我尝试了之前提出的所有问题的每个答案,但没有任何效果。

根据docs失败的原因有多种,主要是

failure to call release()

建议您发现错误并恢复。

Some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like. Thus, error reporting and recovery is an important concern under these circumstances.

试试这个 answer 或更好的东西来捕获错误。或者试试下面的代码,释放内存中的对象。

mSound = MediaPlayer.create(getApplicationContext(), R.raw.button);
mSound.start();

mSound.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.release();

    };
});

我在重复播放一些音频文件时遇到了同样的错误。 我正在制作一本简单的英语到法语短语书,有 8 个按钮用于 8 个常用短语,音频文件非常小。 在密切监视日志时,我发现代码缓存的增量存在一些限制。 一段时间后反复播放音频后,我得到了这个:

2019-12-10 12:37:32.561 29888-29893/com.example.gridlayout I/zygote: Do partial code cache collection, code=22KB, data=30KB
2019-12-10 12:37:32.567 29888-29893/com.example.gridlayout I/zygote: After code cache collection, code=22KB, data=30KB
2019-12-10 12:37:32.567 29888-29893/com.example.gridlayout I/zygote: Increasing code cache capacity to 128KB

在进一步重复之后,我得到了这个:

2019-12-10 12:37:46.008 29888-29893/com.example.gridlayout I/zygote: Do partial code cache collection, code=53KB, data=57KB
2019-12-10 12:37:46.009 29888-29893/com.example.gridlayout I/zygote: After code cache collection, code=53KB, data=57KB
2019-12-10 12:37:46.009 29888-29893/com.example.gridlayout I/zygote: Increasing code cache capacity to 256KB

在做更多的事情后,我得到了:

2019-12-10 12:38:23.596 29888-29905/com.example.gridlayout E/MediaPlayerNative: error (1, -19)
2019-12-10 12:38:23.596 29888-29888/com.example.gridlayout E/MediaPlayer: Error (1,-19)

我通过耐心等待短音频完成然后再次播放来重复此操作,确保缓存不会因同时播放一遍又一遍而增加。

我推断的是,对于运行巨大的音频文件,需要增加代码缓存,它会在一定程度上自动执行并在限制后给出错误(我的情况是256KB) .很确定可以以某种方式增加缓存...

我在我的 phone 上试验了稳健性,它运行得非常好,我想在我们的 phone 中,它们会在完成后自动清除缓存。 尝试 运行在移动设备而不是模拟器上安装您的应用程序。

我的结论:代码缓存限制了声音播放的性能。要么增加代码缓存限制(我试过浅搜索,找不到,但搜索得当,我想你会在某个地方找到它),或者尝试直接在你的手机上 运行ning 它。