4

I am having trouble with playing multiple sound effects using the SoundPool class. Basically I am making 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...

Dan
  • 842
  • 3
  • 17
  • 29

2 Answers2

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

needs to be

mSoundPool = new SoundPool(mBalloonPopCount, AudioManager.STREAM_MUSIC, 0);
Dominic
  • 31
  • 2
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;
Carlos Silva
  • 544
  • 3
  • 12
  • Have you tried with another format of audio file? like mp3 or ogg? it can be the file format. – Carlos Silva Aug 15 '11 at 02:28
  • Yes and it doesn't work. I've even tried other sound files in varying formats and I still get the error. – Dan Aug 15 '11 at 03:14