MediaPlayer的缺点:
资源占用量高,延时时间较长
不支持多个音效同时播放
SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音量,播放比率等参数,避免使用SoundPool来播放歌曲或者做游戏背景音乐,只有那些短促的密集的声音才考虑使用SoundPool播放
构造器:
public SoundPool (int maxStreams, int streamType, int srcQuality)
Parameters
maxStreams the maximum number of simultaneous streams for this SoundPool object
streamType the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.
srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.
Returns
a SoundPool object, or null if creation failed
加载声音:
public int load (AssetFileDescriptor afd, int priority)
Parameters
afd an asset file descriptor
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (Context context, int resId, int priority)
Parameters
context the application context
resId the resource ID
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (String path, int priority)
Parameters
path the path to the audio file
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (FileDescriptor fd, long offset, long length, int priority)
Parameters
fd a FileDescriptor object
offset offset to the start of the sound
length length of the sound
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
播放声音:
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
Parameters
soundID a soundID returned by the load() function
leftVolume left volume value (range = 0.0 to 1.0)
rightVolume right volume value (range = 0.0 to 1.0)
priority stream priority (0 = lowest priority)
loop loop mode (0 = no loop, -1 = loop forever)
rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
使用SoundPool播放声音的步骤:
调用SoundPool的构造器创建SoundPool的对象
调用SoundPool对象的load()方法从指定资源,文件,中加载声音,最好使用HashMap<Integer,Integer>来管理所加载的声音
调用SoundPool的play方法播放声音
例子程序:
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SoundPoolTest extends Activity implements OnClickListener
{
Button bomb, shot, arrow;
// 定义一个SoundPool
SoundPool soundPool;
HashMap<Integer, Integer> soundMap =
new HashMap<Integer, Integer>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bomb = (Button) findViewById(R.id.bomb);
shot = (Button) findViewById(R.id.shot);
arrow = (Button) findViewById(R.id.arrow);
// 设置最多可容纳10个音频流,音频的品质为5
soundPool = new SoundPool(10
, AudioManager.STREAM_SYSTEM, 5); //①
// load方法加载指定音频文件,并返回所加载的音频ID。
// 此处使用HashMap来管理这些音频流
soundMap.put(1, soundPool.load(this, R.raw.bomb, 1)); //②
soundMap.put(2, soundPool.load(this, R.raw.shot, 1));
soundMap.put(3, soundPool.load(this, R.raw.arrow, 1));
bomb.setOnClickListener(this);
shot.setOnClickListener(this);
arrow.setOnClickListener(this);
}
// 重写OnClickListener监听器接口的方法
@Override
public void onClick(View source)
{
// 判断哪个按钮被单击
switch (source.getId())
{
case R.id.bomb:
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1); //③
break;
case R.id.shot:
soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1);
break;
case R.id.arrow:
soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1);
break;
}
}
}
分享到:
相关推荐
使用 SoundPool 播放音效,SoundPool 还支持自行设置声音的品质、音量、 播放比率等参数
Android设备的一些较短的声音可以用SoundPool来做,如消息提醒等,此例子介绍了SoundPool的用法。
Android SoundPool音效使用Demo源码.rar
如果在程序应用中(比如:游戏的音效等)需要播放密集、短促的音效,这时就使用SoundPool来播放音效,SoundPool使用音效池的概念来管理多个短促的音效,例如它可以开始就10个音效,以后在程序中按音效的ID进行播放。...
本文实例为大家分享了Android使用SoundPool播放音效的具体代码,供大家参考,具体内容如下 SoundPool(int maxStreams, int streamType, int srcQuality) 参数依次是: ①指定支持多少个声音,SoundPool对象中允许...
本篇文章主要介绍了android使用SoundPool播放音效的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要为大家详细介绍了Android使用SoundPool播放音效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了Android使用SoundPool播放短音效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
资源为一个专门处理短音效soundPool的工具类,使用起来很方便,封装的很好!使用方法: 1、你只需要在需要的地方调用这个方法进行类初始化 private void initSound() { soundPlay = new SoundPlay(); soundPlay....
这种简单的音乐的播放就运用到了我们的SoundPool,它使用音效池的概念来管理短促的音效,例如它可以开始就加载20 个音效,通过他们的id进行管理与播放。SoundPool的优势在于占用的CPU资源少,反应延迟降低。另外它还...
android背景音乐、背景音效实例,其中大音频音乐播放使用MediaPlayer实现,小音频音效播放使用SoundPool实现
代码中使用SoundPool播放多个音效。
安卓Android源码——(SoundPool音效).zip
安卓Android源码——(SoundPool音效).rar
本文实例讲述了Android编程实现使用SoundPool播放音乐的方法。分享给大家供大家参考,具体如下: 如果应用程序要播放密集、短促的音效,这时还用MediaPlayer就显得不合适了。MediaPlayer存在如下缺点: 1、资源占用...