Android SoundPool无法打开很多曲目/如何创建CBR OGG文件?

时间:2014-10-23 19:30:16

标签: android soundpool ogg audacity

我正在尝试在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

我在谷歌上做了一些研究,最终以OGG格式保存了MP3文件并尽可能缩短了它们的持续时间。每个文件的持续时间约为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);

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

    // ...

}

1 个答案:

答案 0 :(得分: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);

    }
};
}