public static final int SOUND_EXPLOSION = 1;

private SoundPool soundPool;
private HashMap<Integer, Integer> soundPoolMap;

private void initSounds() {
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion, 1));
}

public void playSound(int sound) {
/* Updated: The next 4 lines calculate the current volume in a scale of 0.0 to 1.0 */
AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;

/* Play the sound with the correct volume */
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
}

public void explode() {
playSound(SOUND_EXPLOSION);
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android中的ImageButton标签属性
  2. Android多渠道打包(四):360多渠道打包
  3. android:layout_marginLeft android:padd
  4. Android中的ImageButton标签属性
  5. Android(安卓)API Level对应Android版本
  6. Android(安卓)SDK Android(安卓)NDK 官方
  7. Android(安卓)Animation无限循环动画
  8. Android中设置activity的background为Wal
  9. Android(安卓)内核/框架结构学习笔记
  10. android:versionCode="1"与android:versi