手机播放声音和震动
来源:互联网 发布:洛阳青峰网络黄伟 编辑:程序博客网 时间:2024/04/28 07:09
在游戏开发时,加上声音和震动会让游戏看起来非常上等。
首先,播放声音:
public class SoundManager { private static SoundManager soundManager; private Context context; private SoundPool soundPool; private HashMap<Integer, Integer> soundPoolMap; public static final int SHOOT=1; private SoundManager(Context context){ soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); soundPoolMap = new HashMap<Integer, Integer>(); soundPoolMap.put(SHOOT, soundPool.load(context, R.raw.bulletsound, 1)); this.context = context; } public static SoundManager newInstance(Context context){ if(soundManager == null){ soundManager = new SoundManager(context); } return soundManager; } public void playSound(int type){ AudioManager mgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = streamVolumeCurrent / streamVolumeMax; /* 使用正确音量播放声音 */ soundPool.play(soundPoolMap.get(type), volume, volume, 1, 0, 1f); }}声音文件加载只需要加在一起,所以我把SoundManager设计为单例的。
震动:
Vibrator vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);vibrator.vibrate(new long[] {100, 10, 10, 100}, -1); //-1短震动
注意需要在AndroidManifest.xml文件中加入下面这行:
<uses-permission android:name="android.permission.VIBRATE" />
- 手机播放声音和震动
- ios 播放声音和震动
- IOS 播放声音和震动
- iOS 播放声音和震动
- iOS 播放声音和震动
- 播放系统声音&震动
- ios 播放声音 播放震动
- 关于ios里的摇一摇以及播放声音和震动
- i os 系统震动 和 播放系统声音
- iOS收到Push后播放声音和震动
- 手机的声音与震动
- 震动和声音
- 震动和声音
- 短音频\音乐播放+震动(和手机来电一样震动音乐停震动停)
- Unity3d之音效播放和调用手机震动
- j2me播放声音,震动,背光灯闪烁
- android实现震动和声音
- 震动和系统声音提示
- 单链表
- ccsu1359 木棒相交 (叉积线段判交,并查集判断是否属于同一个集合)
- dede 序号 递增
- js实现图片上传预览
- Windows Phone 7开发之ToggleSwitch-兵乓开关
- 手机播放声音和震动
- 配置yum源【附脚本】
- POJ 1830 开关问题 高斯消元
- C++中为什么需要多态
- 数组作为参数传递时,注意重载函数!
- <转载> 设计模式精解c++-GoF 23 种设计模式解析
- 《程序员面试宝典》第二版的 错误修正
- POJ 1936
- 安个窝纪念