java 手机声音提醒功能_java – 同时播放声音Android
我在
Android上創造了一個游戲,而且我已經把這個問題放在一段時間了,現在我剛剛回來了.在我的游戲中,我有一個背景拍攝,槍聲,爆炸等等,我需要能夠同時玩.現在,當我在SoundPool類上打電話時,當前播放的聲音被中斷,新的播放聲音開始播放.我的SoundManager類在下面以及用法.任何幫助將不勝感激,因為這真的是我的第一個游戲,我需要這么多的音效.謝謝!
public class SoundManager {
private SoundPool mSoundPool;
private HashMap mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public SoundManager(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, 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 playSound(int index) {
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
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);
}
}
…這里是我如何使用類的一個例子.
SoundManager sm = new SoundManager(this);
sm.addSound(0, R.raw.explosion);
sm.playSound(0);
…所以用這種風格,我把所有的聲音添加到SoundPool上,然后根據用戶輸入,我只想播放聲音.這看起來正確嗎?或者我應該嘗試不同的方法嗎?
總結
以上是生活随笔為你收集整理的java 手机声音提醒功能_java – 同时播放声音Android的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: map转换成JSON的3种方式
- 下一篇: nef格式如何换成jpg格式