SoundPool简单使用
2016-02-15 14:51
288 查看
public class SoundPoolAct extends Activity { private SoundPool mSoundPool;// SoundPool适合播放短促的声音,如手机按键声音,短促铃声等 private int soundId; private Button play; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sound_pool); play = (Button) findViewById(R.id.play); // 第一步:新建SoundPool对象:public SoundPool (int maxStreams, int streamType, int srcQuality) mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);// This constructor was deprecated in API level 21. use SoundPool.Builder instead to create and configure a SoundPool instance 参数说明: maxStream —— 同时播放的流的最大数量 streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出) srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值 //第二步:加载声音源文件,共有四种方式,返回的都是对应的sound ID,使用这个id来操作对应的声音 1,public int load (AssetFileDescriptor afd, int priority) 2,public int load (Context context, int resId, int priority) 3,public int load (String path, int priority) 4,public int load (FileDescriptor fd, long offset, long length, int priority) soundId = mSoundPool.load(this, R.raw.music, 1);// 第三个参数priority参数目前未使用,建议设置为1。 // 第三步:播放操作:public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) play.setOnClickListener(new OnClickListener() { @Override public void onClick(View paramView) { mSoundPool.play(soundId, 1, 1, 0, -1, 1); 参数说明: soundID a soundID returned by the load() function要播放的声音id 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)循环播放的次数,0为值播放一次,-1为无限循环 rate playback rate (1.0 = normal playback, range 0.5 to 2.0)播放的速率 } }); } }
其它常用方法:
1.暂停操作:public final void pause (int streamID)
2.释放资源:public final void release ()
3.设置播放循环模式:public final void setLoop (int streamID, int loop)
4.停止播放:public final void stop (int streamID)
相关文章推荐
- h:outputText (escape属性)输出HTML标签
- 如何清除xcode里面的mobileprovision文件
- 音频编解码·格式篇(1)Wave PCM audio format(WAV)
- select、poll、epoll之间的区别总结[整理]
- script标签的type="test/html"时
- mysql 64 zip download
- 【Python】使用python的tornado配合html页面示例
- Android 微信摇骰子
- HDU 2262 Where is the canteen 期望 + 高斯消元
- 哪种语言合适写爬虫程序
- Oracle中as关键字
- 调用相册
- 【JSTL】--c:out演示--drp213
- Pseudoprime numbers(POJ 3641 快速幂)
- Linux学习之路——启程 、心灵的鸡汤
- modeler链接sql server
- android程序右上角ActionBar上显示三个点(overflow)
- Pentaho BI Server的启动过程详解(一)
- CentOS7 Magento 链接Redis提示:Connection to Redis failed after 2 failures
- HTML网页字体动态显示