从头学Android之多媒体--使用SoundPool播放音频
SoundPool
构造方法
构造方法 | 描述 |
public SoundPool (int maxStreams, int streamType, int srcQuality) | 参数说明: maxStreams:指定支持多少个文件 streamType:指定声音类型 srcQuality:声音品质 |
常见方法
方法名称 | 描述 |
public int load (Context context, int resId, int priority) | 从资源ID所对应的资源加载声音 |
public int load (AssetFileDescriptor afd, int priority) | 从原始资源文件中加载声音 |
public int load (FileDescriptor fd, long offset, long length, int priority) | 从原始资源文件中加载声音并设置加载从哪开始到多长的声音文件 |
public int load (String path, int priority) | 从指定文件路径加载声音 |
public final void pause (int streamID) | 暂停 |
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) | 播放 参数说明: soundID:资源ID leftVolume:左频道声音 rightVolume:右频道声音 loop:-1代表循环,0代表不循环 rate:值0.5-2.0设置1为正常 |
public final void release () | 释放SoundPool对象资源 |
public final void stop (int streamID) | 停止 |
public void setOnLoadCompleteListener (SoundPool.OnLoadCompleteListener listener) | 设置监听器,在加载音乐文件完成时触发该事件 |
- packagecom.jiahui.soundpool;
- importjava.util.HashMap;
- importjava.util.Map;
- importandroid.app.Activity;
- importandroid.media.AudioManager;
- importandroid.media.SoundPool;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- publicclassSoundPoolDemoActivityextendsActivityimplementsOnClickListener{
- privateButtonbtnbomb,btnshot,btnarrow;
- privateSoundPoolsoundPool;
- Map<Integer,Integer>soundMap=newHashMap<Integer,Integer>();
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btnbomb=(Button)this.findViewById(R.id.btnbomb);
- btnshot=(Button)this.findViewById(R.id.btnshot);
- btnarrow=(Button)this.findViewById(R.id.btnarrow);
- btnbomb.setOnClickListener(this);
- btnshot.setOnClickListener(this);
- btnarrow.setOnClickListener(this);
- //创建SoundPool对象设置最多容纳10个音频。音频的品质为5
- soundPool=newSoundPool(10,AudioManager.STREAM_SYSTEM,5);
- //load方法加载音频文件返回对应的ID
- 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));
- }
- @Override
- publicvoidonClick(Viewv){
- switch(v.getId()){
- caseR.id.btnbomb:
- soundPool.play(soundMap.get(1),1,1,1,0,1);
- break;
- caseR.id.btnshot:
- soundPool.play(soundMap.get(2),1,1,1,0,1);
- break;
- caseR.id.btnarrow:
- soundPool.play(soundMap.get(3),1,1,1,0,1);
- break;
- default:
- break;
- }
- }
- }
更多相关文章
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- 一款霸榜 GitHub 的开源 Linux 资源监视器!
- Android(安卓)使用WebView 加载新闻详情,添加点击图片的js(使用的
- Android加载webview 弹出错误
- 实现android按键震动按键声音的机制
- Android中picasso图片缓存
- 编写android加载图片的程序时,遇到了内存泄露问题!
- Frame Animation&&Drawable Animation
- android ubuntu9.10 源码的编译 Eclipse工程 Contacts编译 应用