gpt4 book ai didi

java - 在 soundpool 中播放多个实例

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:52 24 4
gpt4 key购买 nike

如何播放下面代码中放置的同一音乐的多个实例?另外,如果我同时播放两种不同的声音,我该在哪里放置该代码?

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

}
});
}

最佳答案

示例代码:

Music music = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath);
Music music2 = MusicFactory.createMusicFromAsset(pMusicManager, pContext, pAssetPath);

music.play;
music2.play;

关于java - 在 soundpool 中播放多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com