android开发分享Android使用SoundPool播放音效实例

使用场景 soundpool一般用来 播放密集,急促而又短暂的音效,比如特技音效:duang~,游戏用得较多,你也可以为你的 app添加上这个音效,比如酷狗音乐进去的时候播放”哈

使用场景

soundpool一般用来 播放密集,急促而又短暂的音效,比如特技音效:duang~,游戏用得较多,你也可以为你的 app添加上这个音效,比如酷狗音乐进去的时候播放”哈喽,酷狗” 是不是提起了对于soundpool的兴趣了呢

ok,废话不多说 详细的参数解释请看注释

  public class soundplayer extends appcompatactivity {      private soundpool msoundpool;        @override    protected void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);      setcontentview(r.layout.activity_sound_player);        initstate();    }      private void initstate() {      //sdk版本21是soundpool 的一个分水岭      if (build.version.sdk_int >= 21) {        soundpool.builder builder = new soundpool.builder();        //传入最多播放音频数量,        builder.setmaxstreams(1);        //audioattributes是一个封装音频各种属性的方法        audioattributes.builder attrbuilder = new audioattributes.builder();        //设置音频流的合适的属性        attrbuilder.setlegacystreamtype(audiomanager.stream_music);        //加载一个audioattributes        builder.setaudioattributes(attrbuilder.build());        msoundpool = builder.build();      } else {        /**         * 第一个参数:int maxstreams:soundpool对象的最大并发流数         * 第二个参数:int streamtype:audiomanager中描述的音频流类型         *第三个参数:int srcquality:采样率转换器的质量。 目前没有效果。 使用0作为默认值。         */        msoundpool = new soundpool(1, audiomanager.stream_music, 0);      }        //可以通过四种途径来记载一个音频资源:      //context:上下文      //resid:资源id      // priority:没什么用的一个参数,建议设置为1,保持和未来的兼容性      //path:文件路径      // filedescriptor:貌似是流吧,这个我也不知道      //:从asset目录读取某个资源文件,用法: assetfiledescriptor descriptor = assetmanager.openfd("biaobiao.mp3");        //1.通过一个assetfiledescriptor对象      //int load(assetfiledescriptor afd, int priority)      //2.通过一个资源id      //int load(context context, int resid, int priority)      //3.通过指定的路径加载      //int load(string path, int priority)      //4.通过filedescriptor加载      //int load(filedescriptor fd, long offset, long length, int priority)      //声音id 加载音频资源,这里用的是第二种,第三个参数为priority,声音的优先级*api中指出,priority参数目前没有效果,建议设置为1。      final int voiceid = msoundpool.load(this, r.raw.duang, 1);      //异步需要等待加载完成,音频才能播放成功      msoundpool.setonloadcompletelistener(new soundpool.onloadcompletelistener() {        @override        public void onloadcomplete(soundpool soundpool, int sampleid, int status) {          if (status == 0) {            //第一个参数soundid            //第二个参数leftvolume为左侧音量值(范围= 0.0到1.0)            //第三个参数rightvolume为右的音量值(范围= 0.0到1.0)            //第四个参数priority 为流的优先级,值越大优先级高,影响当同时播放数量超出了最大支持数时soundpool对该流的处理            //第五个参数loop 为音频重复播放次数,0为值播放一次,-1为无限循环,其他值为播放loop+1次            //第六个参数 rate为播放的速率,范围0.5-2.0(0.5为一半速率,1.0为正常速率,2.0为两倍速率)            soundpool.play(voiceid, 1, 1, 1, 0, 1);          }        }      });    }    }

非常简单的使用。

以上就是android开发分享Android使用SoundPool播放音效实例的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/899720.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐