Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

android - Playing multiple sounds using SoundManager

If I play a single sound, it runs fine.

Adding a second sound causes it to crash.

Anyone know what is causing the problem?

private SoundManager mSoundManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sos);

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

    mSoundManager.addSound(1,R.raw.dit);
    mSoundManager.addSound(1,R.raw.dah);

    Button SoundButton = (Button)findViewById(R.id.SoundButton);
    SoundButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mSoundManager.playSound(1);
            mSoundManager.playSound(2);
        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 mSoundManager.addSound(1,R.raw.dit);
 mSoundManager.addSound(1,R.raw.dah);

You need to change the second line to:

mSoundManager.addSound(2,R.raw.dah);

In order to play multiple sounds at once, first you need to let the SoundPool know that. In the declaration of SoundPool notice that I specified 20 streams. I have many guns and bad guys making noise in my game, and each has a very short sound loop, < 3000ms. Notice when I add a sound below I keep track of the specified index in a vector called, "mAvailibleSounds", this way my game can try and play sounds for items that dont exist and carry on without crashing. Each Index in this case corresponds to a sprite id. Just so you understand how I map particular sounds to specific sprites.

Next we queue up sounds, with playSound(). Each time this happens a soundId is dropped into a stack, which I then pop whenever my timeout occurs. This allows me to kill a stream after it has played, and reuse it again. I choose 20 streams because my game is very noisy. After that the sounds get washed out, so each application will require a magic number.

I found this source here, and added the runnable & kill queue myself.

private  SoundPool mSoundPool; 
 private  HashMap<Integer, Integer> mSoundPoolMap; 
 private  AudioManager  mAudioManager;
 private  Context mContext;
 private  Vector<Integer> mAvailibleSounds = new Vector<Integer>();
 private  Vector<Integer> mKillSoundQueue = new Vector<Integer>();
 private  Handler mHandler = new Handler();

 public SoundManager(){}

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

 public void addSound(int Index, int SoundID)
 {
  mAvailibleSounds.add(Index);
  mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

 }

 public void playSound(int index) { 
  // dont have a sound for this obj, return.
  if(mAvailibleSounds.contains(index)){

      int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
      int soundId = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);

      mKillSoundQueue.add(soundId);

      // schedule the current sound to stop after set milliseconds
      mHandler.postDelayed(new Runnable() {
       public void run() {
        if(!mKillSoundQueue.isEmpty()){
         mSoundPool.stop(mKillSoundQueue.firstElement());
        }
          }
      }, 3000);
  }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...