Android Soundpool无法播放声音

时间:2011-08-14 23:57:21

标签: java android android-logcat

使用SoundPool类播放多种音效时遇到问题。基本上我正在制作一个实时游戏,涉及用户在屏幕上点击一堆对象。每当玩家触摸一个物体时,我都需要播放声音。所以我转向SoundPool类。以下是我的代码:

private SoundPool mSoundPool;
private int mSoundId;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PreferencesManager.getInstance(this);

    setContentView(R.layout.arcademode);
    initialize();
}

private void initialize() {
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mSoundPool = new SoundPool(mBalloonPopCount, R.raw.pop, 0);
    mSoundId = mSoundPool.load(this, R.raw.pop, 1);
}

private void playSound() {
    float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    float volume = actualVolume / maxVolume;
    mSoundPool.play(mSoundId, volume, volume, 1, 0, 1f);
}

现在每当调用playSound()方法时,我都没有听到任何声音。当我查看LogCat时,我看到以下几行:

08-14 19:48:50.117: ERROR/AudioFlinger(2613): invalid stream type
08-14 19:48:50.117: ERROR/AudioTrack(15611): AudioFlinger could not create track, status: -22
08-14 19:48:50.117: ERROR/SoundPool(15611): Error creating AudioTrack

相关音频文件是 .wav 文件。我还尝试了 .ogg .mp3 文件,它们也无法正常工作。声音文件非常小(.wav文件甚至不占用60KB)所以尺寸不是问题......

3 个答案:

答案 0 :(得分:3)

mSoundPool = new SoundPool(mBalloonPopCount, R.raw.pop, 0);

需要

mSoundPool = new SoundPool(mBalloonPopCount, AudioManager.STREAM_MUSIC, 0);

答案 1 :(得分:0)

从使用SoundPool的每个例子中,音量部分与您拥有的不同。就像这样:

float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;

答案 2 :(得分:0)

不久前有同样的问题。在我的情况下杀死系统内的所有过程有帮助。不过,我不知道发生了什么,问题的原因是什么。