0

我试图弄清楚如何在应用程序中加载各种声音。我的声音管理器工作顺利,如下所示:

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



}

我的应用程序的打开活动包含以下代码片段,以使声音正常工作:

public SoundManager mSoundManager;

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

以下代码播放随机声音(有效,但仅在开放活动中):

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

mSoundManager.playSound(b);

所以我的困境是我需要能够一次加载所有活动的 WritingSounds,因为现在,如果我尝试在另一个活动上播放声音,除非我添加代码片段来制作音响经理工作。

我该怎么办!?

4

0 回答 0