如何在下面的代码中播放相同音乐的多个实例?此外,如果我一次播放2种不同的声音,我在哪里放置该代码?
public class SoundManager {
private SoundPool msoundPool;
private HashMap<Integer, Integer> mSoundPoolMap;
private AudioManager maudioManager;
private Context mContext;
public SoundManager()
{
}
public void initSounds(Context theContext)
{
mContext=theContext;
msoundPool=new SoundPool(4, AudioManager.STREAM_MUSIC,0);
mSoundPoolMap=new HashMap<Integer, Integer>();
maudioManager=(AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSounds(int Index,int soundId)
{
mSoundPoolMap.put(1, msoundPool.load(mContext, soundId,1));
}
public void playSound(int index)
{
int streamVolume=maudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
msoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index)
{
int streamVolume=maudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
msoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1,1f);
}
}
我的主要方法如下:我在onCreate方法
中调用了它 m=new SoundManager();
m.initSounds(getBaseContext());
m.addSounds(1, R.raw.sound);
Button button=(Button)findViewById(R.id.button11);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m.playSound(1);
}
});
}
答案 0 :(得分:0)
示例代码:
Music music = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath);
Music music2 = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath);
music.play;
music2.play;