在Android中同时播放多个声音
我无法使用以下代码同时播放多个声音/蜂鸣声。在我的 onclicklistener
中,我添加了:
public void onClick(View v) {
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
但这一次只播放一种声音,索引为 1 的声音,然后是索引为 2 的声音。
每当有时,如何使用此代码同时播放至少 2 个声音onClick()
事件?
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 addSound(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);
}
}
I am unable to use the following code to play multiple sounds/beeps simultaneously. In my onclicklistener
I have added:
public void onClick(View v) {
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
But this plays only one sound at a time, sound with index 1 followed by sound with index 2.
How can I play at least 2 sounds simultaneously using this code whenever there is an onClick()
event?
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 addSound(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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用声音池播放声音效果的类如下:
它确实同时播放注释“测试同时播放”下面提到的 3 个声音。看来您需要在我的代码监听器(
OnLoadCompleteListener
)中实现注释。可能会发生系统在您开始播放时未加载声音的情况。如果您不想实现提到的侦听器,请尝试在播放声音活动开始后等待一段时间,并在一段时间后开始播放声音。Having the following class to play sound fx using sound pool:
it DOES play simultaneously 3 sounds mentioned below the comment "testing simultaneous playing". It seems like you need to implement commented in my code listener (
OnLoadCompleteListener
). It could happen that system just didn't load sounds atm you start playing it. If you dont want to implement mentioned listener try to wait some time after your playing sounds activity started and start playing sounds after a while.