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 开发报错处理
  2. android 8.1launcher去掉抽屉,显示allapp
  3. Android的VideoView如何循环播放
  4. android的samples
  5. android Sqlite数据库事务处理*.sql 语句
  6. android的WebView简单实例
  7. Android 网页加载完成ProgressDialog运用
  8. Android CheckBox使密码可见/隐藏
  9. How to add custom title bar to android
  10. Android悬浮框