0

我正在尝试在 SoundPool 中加载一些曲目并根据某些条件在它们之间切换。

在开始时,甚至第一首曲目都不会开始播放,并显示以下错误:

10-23 22:11:07.240: E/AudioTrack(16803): AudioFlinger could not create track, status: -12
10-23 22:11:07.240: E/SoundPool(16803): Error creating AudioTrack

我在 Google 上做了一些研究,最终将 MP3 文件保存为 OGG 格式,并尽可能缩短了它们的持续时间。每个文件的持续时间约为 3 秒,我使用相同的 SoundPool 对象将它们置于无限循环中。我使用 Audacity 以 OGG 格式导出它们,但 Audacity 不支持 ConstantBitRate,根据我发现的有关 SoundPool 的几篇文章/问题,我应该使用它。

这允许加载 2 个声音文件,但是当我尝试启动第 3 个文件时,此错误再次出现。

10-23 22:11:07.240: E/AudioTrack(16803): AudioFlinger could not create track, status: -12
10-23 22:11:07.240: E/SoundPool(16803): Error creating AudioTrack

如何让所有 3 个文件同时播放?

编辑:活动的代码如下。

private SoundPool soundPool;
private AudioManager audioManager;
private float actVolume;
private float maxVolume;
private float volume;
private boolean mainPlaying;
private Integer clean = -1;
private Integer filter1 = -1;
private Integer filter2 = -1;
private Integer[] mainAudios = new Integer[] { clean, filter1, filter2 };
private int remaining;

// ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // /////////////////////////////// AUDIO
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    actVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    volume = actVolume / maxVolume;
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            remaining--;
            if (remaining == 0) {
                mainAudios = new Integer[] { clean, filter1, filter2 };
                for (int id : mainAudios)
                    soundPool.play(id, 0, 0, 1, -1, 1f);
                mainPlaying = true;
            }
        }
    });

    remaining = mainAudios.length;
    clean = soundPool.load(this, R.raw.angle_normal, 1);
    filter1 = soundPool.load(this, R.raw.filter2, 1);
    filter2 = soundPool.load(this, R.raw.filter5, 1);

    // //////////////////////////////////////////////////

    // ...

}
4

1 回答 1

0

状态:-12 是某种内存错误。您有 3 个声音文件,这很好,但根据设备内存存在内存限制。我会将您的声音文件缩小到每个 25kb 或接近的大小。尝试小文件,它应该可以工作。我使用了自己的声音文件,这些文件非常小,而且效果很好。但是当我放入大文件时,我得到了状态 -12 错误。

如果您想要一个示例,说明我将如何构建您的代码:

public class MainActivity extends Activity {

private AudioManager audioManager;
private float actVolume;
private float maxVolume;
private float volume;

SoundPool sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    actVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    volume = actVolume / maxVolume;
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    sp.release();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

//put your own resources here
        int[] resources = {R.raw.whatsup, R.raw.monkey,
                R.raw.sneeze, R.raw.aww };
        sp = new SoundPool(resources.length, AudioManager.STREAM_MUSIC, 0);
        sp.setOnLoadCompleteListener(listener);

        for (int id : resources)
            sp.load(this, id, 1);

    }
OnLoadCompleteListener listener = new OnLoadCompleteListener() {
    String TAG = "main";

    @Override
public void onLoadComplete(SoundPool soundPool, int id, int status) {

            if (status == 0)
                switch (id) {
    //this switch is just to demo you can collect your variables here for each sound id thats loaded

            case 1:
                    Log.d(TAG, "sound id:"+ id +" loaded");
                    break;
                case 2:
                    Log.d(TAG, "sound id:"+ id +" loaded");
                    break;
            case 3:
                Log.d(TAG, "sound id:"+ id +" loaded");
                break;

            case 4:
                Log.d(TAG, "sound id:"+ id +" loaded");
                break;

            }
        Log.d(TAG, "playing:" + id);
        soundPool.play(id, volume, volume, 0, -1, 1f);

    }
};
}
于 2014-10-24T01:04:31.800 回答