使用方法:
1. 创建一个SoundPool
maxStream —— 同时播放的流的最大数量
streamType —— 流的类型,一般为STREAM_MUSIC(具体在AudioManager类中列出)
srcQuality —— 采样率转化质量,当前无效果,使用0作为默认值
例:
SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
创建了一个最多支持3个流同时播放的,类型标记为音乐的SoundPool。
2 一般把多个声音放到HashMap中去,比如
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
soundpool的加载:
int load(Context context, int resId, int priority) //从APK资源载入
int load(FileDescriptor fd, long offset, long length, int priority) //从FileDescriptor对象载入
int load(AssetFileDescriptor afd, int priority) //从Asset对象载入
int load(String path, int priority) //从完整文件路径名载入
最后一个参数为优先级。
3 播放
play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ,其中leftVolume和rightVolume表示左右音量,priority表示优先级,loop表示循环次数,rate表示速率,如
//速率最低0.5最高为2,1代表正常速度
sp.play(soundId, 1, 1, 0, 0, 1);
而停止则可以使用 pause(int streamID) 方法,这里的streamID和soundID均在构造SoundPool类的第一个参数中指明了总数量,而id从0开始。
最后
以上就是土豪金鱼最近收集整理的关于安卓开发——SoundPool的使用的全部内容,更多相关安卓开发——SoundPool内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复