0

I am trying to figure out how to load a variety of sounds in an app. I have my soundmanager working smoothly, shown below:

package com.beeseries.contextclues;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

private  SoundPool mSoundPool;
private  HashMap mSoundPoolMap;
private  AudioManager  mAudioManager;
private  Context mContext;

public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        }

public void addSound(int index, int SoundID)
{
mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
}

public void loadWritingSounds(){
mSoundPoolMap.put(0, mSoundPool.load(mContext, R.raw.writing1, 1));
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.writing2, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.writing3, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.writing4, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.writing5, 1));
}

public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume /     mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0,     1f);
}

public void playLoopedSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}



}

My opening activity for my app contains the following snippets of code to make the sounds work:

public SoundManager mSoundManager;

mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.loadWritingSounds();

And the following code to play a random sound (WHICH DOES WORK, BUT ONLY ON THE OPENING ACTIVITY):

int b = randSound1.nextInt((whichSound.length));

mSoundManager.playSound(b);

So my dilemma is that I need to be able to load the WritingSounds for ALL activities at once, because right now, if I try to play a sound on another activity, it won't work unless I add the snippets of code to make the soundmanager work.

What do I do!?

codesomethin
  • 195
  • 1
  • 3
  • 11
  • Where does your soundmanager exist? Is it a global object/singleton, or tied into the application somehow? If you want to use it across activities you need to make sure that it lives for the entire lifecycle of the application. If your only reference to the soundmanager is in the initial activity, it will be GC'd when your activity closes. – Tim Nov 28 '12 at 02:27
  • Hmm, not exactly sure what you're asking, but I can tell you that I have a class named SoundManager.java , and my reference in my initial activity is to that class. – codesomethin Nov 28 '12 at 02:34
  • Also, just noticed SoundManager.java is not referenced in my manifest file anywhere – codesomethin Nov 28 '12 at 02:35
  • I believe I need to refer back to the SAME sound manager each time, because I keep doing mSoundManager = new SoundManager(); – codesomethin Nov 28 '12 at 03:35

0 Answers0