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上,然后根据用户输入,我只想播放声音.这看起来正确吗?或者我应该尝试不同的方法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值