使用SoundPool播放游戏音效
2011-12-23 13:47
267 查看
在Android开发中我们经常使用MediaPlayer来播放音频文件,但是MediaPlayer存在一些不足,例如:资源占用量较高、延迟时间较长、不支持多个音频同时播放等。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的游戏开发中。
在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。
SoundPool(android.media.SoundPool),顾名思义是声音池的意思,主要用于播放一些较短的声音片段,支持从程序的资源或文件系统加载。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、播放比率等参数,支持通过ID对多个音频流进行管理。下面是SoundPool基本使用方法的例子代码:
Java代码
public static final int SOUND_EXPLOSION = 1;
public static final int SOUND_YOU_WIN = 2;
public static final int SOUND_YOU_LOSE = 3;
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) {
AudioManager mgr = (AudioManager) getContext().getSystemService(
Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1,
0, 1f);
}
public void update() {
if (isExploding()) {
playSound(SOUND_EXPLOSION);
}
}
遗憾的是SoundPool目前存在的问题还比较多,我们需要谨慎使用,尽量避免程序在调用SoundPool过程中出现异常关闭的情况。相信Google在Android的后续版本中会将这些问题修复(或者推出一个功能类似、健壮度更高的API),但是如果你现在就需要使用SoundPool开发你的程序,也许你需要知道这些问题:
1. SoundPool最大只能申请1M的内存空间,这就意味着我们只能使用一些很短的声音片段,而不是用它来播放歌曲或者游戏背景音乐(背景音乐可以考虑使用JetPlayer来播放)。
2. SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用,因为有些时候它们可能会使你的程序莫名其妙的终止。还有些朋友反映它们不会立即中止播放声音,而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。
3. 音频格式建议使用OGG格式。在我的小游戏Agile Buddy中,我一开始使用WAV格式的音频文件存放游戏音效。经过反复测试,在音效播放间隔较短的情况下会出现异常关闭的情况(有说法是SoundPool目前只对16bit的WAV文件有较好的支持)。后来将文件转成OGG格式,问题得到了解决。
在游戏开发中我们经常需要播放一些游戏音效(比如:子弹爆炸,物体撞击等),这些音效的共同特点是短促、密集、延迟程度小。在这样的场景下,我们可以使用SoundPool代替MediaPlayer来播放这些音效。
SoundPool(android.media.SoundPool),顾名思义是声音池的意思,主要用于播放一些较短的声音片段,支持从程序的资源或文件系统加载。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、播放比率等参数,支持通过ID对多个音频流进行管理。下面是SoundPool基本使用方法的例子代码:
Java代码
public static final int SOUND_EXPLOSION = 1;
public static final int SOUND_YOU_WIN = 2;
public static final int SOUND_YOU_LOSE = 3;
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) {
AudioManager mgr = (AudioManager) getContext().getSystemService(
Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1,
0, 1f);
}
public void update() {
if (isExploding()) {
playSound(SOUND_EXPLOSION);
}
}
遗憾的是SoundPool目前存在的问题还比较多,我们需要谨慎使用,尽量避免程序在调用SoundPool过程中出现异常关闭的情况。相信Google在Android的后续版本中会将这些问题修复(或者推出一个功能类似、健壮度更高的API),但是如果你现在就需要使用SoundPool开发你的程序,也许你需要知道这些问题:
1. SoundPool最大只能申请1M的内存空间,这就意味着我们只能使用一些很短的声音片段,而不是用它来播放歌曲或者游戏背景音乐(背景音乐可以考虑使用JetPlayer来播放)。
2. SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用,因为有些时候它们可能会使你的程序莫名其妙的终止。还有些朋友反映它们不会立即中止播放声音,而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。
3. 音频格式建议使用OGG格式。在我的小游戏Agile Buddy中,我一开始使用WAV格式的音频文件存放游戏音效。经过反复测试,在音效播放间隔较短的情况下会出现异常关闭的情况(有说法是SoundPool目前只对16bit的WAV文件有较好的支持)。后来将文件转成OGG格式,问题得到了解决。
相关文章推荐
- Android游戏开发中使用SoundPool来实现音效的播放
- android 游戏音效播放 SoundPool
- android使用SoundPool 播放短音效
- Android游戏设计之-------游戏音效的播放 SoundPool
- 安卓游戏开发------播放背景音乐和音效(MediaPlayer和SoundPool)
- 初学Android,多媒体之使用SoundPool播放音效(七十六)
- android使用SoundPool播放音效的方法
- Android使用SoundPool播放音效
- Android游戏开发之音效SoundPool的使用
- 使用SoundPool播放音效
- Android 使用SoundPool播放音效
- android使用SoundPool 播放短音效
- 使用SoundPool播放音效
- 使用cocos2d的SimpleAudioEngine播放背景音乐和音效
- android音频播放SoundPool的使用
- 游戏音乐与音效的播放
- Android游戏开发8:使用MediaPlayer类和SoundPool类播放音频
- [Cocos2D-X官方文档]:在Android上使用OpenSL ES来播放音效
- ios微信h5棋牌游戏自动播放音效填坑
- ios之使用录音功能和设置音效播放