编程语言
首页 > 编程语言> > Java-Android Soundpool无法播放声音

Java-Android Soundpool无法播放声音

作者:互联网

我在使用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),所以大小不是问题…

解决方法:

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

需要是

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

标签:android-logcat,java,android
来源: https://codeday.me/bug/20191208/2088566.html