播放时的记忆问题多次停止音频文件

时间:2017-01-24 17:14:36

标签: android memory-management memory-leaks android-mediaplayer

我正在编写一个需要多次播放多个声音文件的游戏。

我正在使用MediaPlayer对象重新定位文件并在需要时每次播放。以下代码我正在使用:

try {
            if (commonPlayer.isPlaying()) {
                commonPlayer.stop();
                commonPlayer.release();
            }

            commonPlayer = null;

            switch (gameAction) {
                case 1:
                    commonPlayer = MediaPlayer.create(context, R.raw.tap);
                    break;

                case 2:
                    commonPlayer = MediaPlayer.create(context, R.raw.score);
                    break;

                case 3:
                    commonPlayer = MediaPlayer.create(context, R.raw.die);
                    break;
            }

            commonPlayer.start();

        } catch (Exception e) {
            log("Exception: " + e.getMessage());
        }

但是在多次播放后,它会停止播放以下错误:

01-24 22:25:51.215   263 28967 I ACodec  : codec does not support config priority (err -2147483648)
01-24 22:25:51.216   263 28967 I MediaCodec: MediaCodec will operate in async mode
01-24 22:25:51.219 22703 22703 I Score   : 3, 15
01-24 22:25:51.220   263 28965 E AudioFlinger: not enough memory for AudioTrack size=131176
01-24 22:25:51.221   263 28965 D MemoryDealer:   AudioTrack (0xa15abbd0, size=4194304)
.......
0x00260980 | 0x00020080 | A 
01-24 22:25:51.221   263 28965 D MemoryDealer:    
01-24 22:25:51.221   263 28965 E AudioFlinger: createTrack_l() initCheck failed -12; no control block?
01-24 22:25:51.221   263 28965 E AudioTrack: AudioFlinger could not create track, status: -12
01-24 22:25:51.221   263 28965 E AudioSink: Unable to create audio track
01-24 22:25:51.221   263 28965 W NuPlayerRenderer: openAudioSink: non offloaded open failed status: -19
01-24 22:25:51.221   263 28962 E NuPlayer: received error(0xffffffed) from audio decoder, flushing(0), now shutting down
01-24 22:25:51.221   263 28962 D NuPlayerDriver: notifyListener_l(0xab839b00), (100, 1, -19)
01-24 22:25:51.221 22703 22773 E MediaPlayer: error (1, -19)
01-24 22:25:51.221   263 28965 W NuPlayerRenderer: onDrainAudioQueue(): audio sink is not ready

有关如何多次播放声音文件的任何建议?

1 个答案:

答案 0 :(得分:1)

而不是

try {
            if (commonPlayer.isPlaying()) {
                commonPlayer.stop();
                commonPlayer.release();
            }

            commonPlayer = null;

            switch (gameAction) {
                case 1:
                    commonPlayer = MediaPlayer.create(context, R.raw.tap);
                    break;

                case 2:
                    commonPlayer = MediaPlayer.create(context, R.raw.score);
                    break;

                case 3:
                    commonPlayer = MediaPlayer.create(context, R.raw.die);
                    break;
            }

            commonPlayer.start();

        } catch (Exception e) {
            log("Exception: " + e.getMessage());
        }

试试这个

commonPlayer = null;

            switch (gameAction) {
                case 1:
                    commonPlayer = MediaPlayer.create(context, R.raw.tap);

          commonPlayer.setOnCompletionListener(new musicCompletionListener());

                    break;

                case 2:
                    commonPlayer = MediaPlayer.create(context, R.raw.score);
commonPlayer.setOnCompletionListener(new musicCompletionListener());

                    break;

                case 3:
                    commonPlayer = MediaPlayer.create(context, R.raw.die);
commonPlayer.setOnCompletionListener(new musicCompletionListener());

                    break;
            }

            commonPlayer.start();

        } 

 private class musicCompletionListener implements MediaPlayer.OnCompletionListener {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        commonPlayer.release();
    }
}