开发者

Android Soundpool fails to play sound

开发者 https://www.devze.com 2023-03-27 21:31 出处:网络
I am having trouble with playing multiple sound effects using the SoundPool class. Basically I am makin开发者_如何学运维g a real time game that involves the user tapping a bunch of objects on the scre

I am having trouble with playing multiple sound effects using the SoundPool class. Basically I am makin开发者_如何学运维g a real time game that involves the user tapping a bunch of objects on the screen. Whenever the player touches an object, I need to play the sound. So I've turned to the SoundPool class. Below is my code:

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);
}

Now whenever the playSound() method is called, I do not hear anything. When I look into LogCat, I see the following lines:

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

The audio file in question is a .wav file. I've also tried .ogg and .mp3 files and they don't work either. The sound file is very small (the .wav file doesn't even take up 60KB) so size isn't the issue...


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

needs to be

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


From every example I sound of using SoundPool, the volume part is different from what you have. It's like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号