为了播放不同的声音,我已经使用soundpool制作了一个SoundManager类,现在我可以通过我的活动点击按钮播放想要的声音。但是我想要一个背景声音,它将在我的整个游戏中播放,从一个活动到另一个活动那么我怎样才能使用SoundPool和我必须编写代码才能在我的活动中播放它。 实际上我是Android的新手,如果有任何其他更好的方式来播放背景声音,请告诉我
这是我的SoundManger类代码。
package com.Tutorial.Sound;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
static private SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap<Integer, Integer> mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;
private SoundManager()
{
}
static synchronized public SoundManager getInstance()
{
if (_instance == null)
_instance = new SoundManager();
return _instance;
}
public static void initSounds(Context theContext)
{
mContext = theContext;
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public static void addSound(int Index,int SoundID)
{
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public static void loadSounds()
{
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.starwars, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.terminator, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.soundfcgo, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.soundfc1, 1));
mSoundPoolMap.put(5, mSoundPool.load(mContext, R.raw.soundfc2, 1));
}
public static void playSound(int index,float speed)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, speed);
}
public static void stopSound(int index)
{
mSoundPool.stop(mSoundPoolMap.get(index));
mSoundPool.pause(mSoundPoolMap.get(index));
}
public static void cleanup()
{
mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
如果你有关于这个背景声音主题的任何例子,那对我来说将是非常充实的