使用SoundPool放声音<wbr style="line-height:25px"><br style="line-height:25px"> 在开发Android软件中我们可能经常需播放多媒体声音文件,一般使用<span style="color:#993300; line-height:25px">MediaPlayer</span>类但该类占用资源较多,<br style="line-height:25px"> 对于游戏等应用可能不是很适合,这里描述下<span style="color:#ff00ff; line-height:25px">SoundPool</span>类。<span style="color:#ff6600; line-height:25px">SoundPool</span>类在SDK的<span style="color:#0000ff; line-height:25px">android.media.SoundPool</span>,<br style="line-height:25px"> 顾名思义是声音池的意思。<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">主要播放一些较短的声音片段,可以从程序的资源或文件系统加载,<br style="line-height:25px"> 相对于</span><span style="color:#993300; line-height:25px">MediaPlayer</span><span style="color:#000080; line-height:25px">类可以做到使用较少的CPU资源和较短的反应延迟。</span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#000080; line-height:25px"></span><wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">SoundPool</span><span style="color:#000080; line-height:25px">和其他声音播放类相比,其特点是可以自行设置声音的品质、音量、播放比率等参等。并且它可以同时管理多个音频流,<br style="line-height:25px"> 每个流都有独自的ID,对某个音频流的管理都是通过ID进行的</span><wbr style="line-height:25px">。SoundPool基本使用方法为:<br style="line-height:25px"> 创建一个SoundPool对象:<span style="color:#993300; line-height:25px">new</span><span style="color:#0000ff; line-height:25px">SoundPool(intmaxStreams,intstreamType,intsrcQuality);</span><br style="line-height:25px"> 从资源或者文件载入音频流:<span style="color:#0000ff; line-height:25px">load(Contextcontext,intresId,intpriority);</span><br style="line-height:25px"> 播放声音<span style="color:#0000ff; line-height:25px">play(intsoundID,floatleftVolume,floatrightVolume,intpriority,intloop,floatrate)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateSoundPoolsndPool;<br style="line-height:25px"> privateHashMap<String,Integer>sndPoolMap=newHashMap<String,Integer>();<br style="line-height:25px"> finalstaticintkSoundMin=R.raw.b1;<br style="line-height:25px"> finalstaticintkSoundMax=R.raw.gameover;<br style="line-height:25px"> finalstaticintkSoundNum=kSoundMax-kSoundMin+1;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">loadAllSounds()<br style="line-height:25px"> {<br style="line-height:25px"> if(sndPool==null)<br style="line-height:25px"> sndPool=newSoundPool(kSoundNum,AudioManager.STREAM_SYSTEM,5);<br style="line-height:25px"> for(inti=kSoundMin;i<=kSoundMax;i++){<br style="line-height:25px"> loadSound(i);<br style="line-height:25px"> }<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">loadSound(intid)<br style="line-height:25px"> {<br style="line-height:25px"> Stringkey=""+id;<br style="line-height:25px"> if(sndPoolMap.containsKey(key))<br style="line-height:25px"> {<br style="line-height:25px"> System.out.println("Waring!thesound"+id+"hasbeenloaded");<br style="line-height:25px"> return;<br style="line-height:25px"> }<br style="line-height:25px"> intindex=sndPool.load(this.getContext(),id,1);<br style="line-height:25px"> sndPoolMap.put(""+id,index);<br style="line-height:25px"> }<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">playSound(intid)<br style="line-height:25px"> {<br style="line-height:25px"> intindex=sndPoolMap.get(""+id);<br style="line-height:25px"> sndPool.play(index,1,1,0,0,1);<br style="line-height:25px"> }<br style="line-height:25px"></span><br style="line-height:25px"> 这样我们就可以使用SoundPool简单的播放一个声音了。相信不少朋友已经想试一试了,<br style="line-height:25px"> 可是SoundPool并非尽善尽美的。就现在已知的资料来说,SoundPool有一些设计上的BUG,从固件版本1.0开始有些都没有修复,<br style="line-height:25px"> 我们在使用中应该小心再小心。相信将来Google会修复这些问题,但我们最好还是列出来:<br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">1.</span><span style="color:#000080; line-height:25px">SoundPool最大只能申请1M的内存空间</span><wbr style="line-height:25px">,这就意味着我们只能用一些很短的声音片段,<br style="line-height:25px"> 而不是用它来播放歌曲或者做游戏背景音乐。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">2.</wbr></span><span style="color:#000080; line-height:25px">SoundPool提供了pause和stop方法,但这些方法建议最好不要轻易使用</span><wbr style="line-height:25px">,<br style="line-height:25px"> 因为有些时候它们可能会使你的程序莫名其妙的终止。<br style="line-height:25px"> Android开发网建议使用这两个方法的时候尽可能多做测试工作,还有些朋友反映它们不会立即中止播放声音,<br style="line-height:25px"> 而是把缓冲区里的数据播放完才会停下来,也许会多播放一秒钟。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">3.</wbr></span><span style="color:#000080; line-height:25px">SoundPool的效率问题。其实SoundPool的效率在这些播放类中算是很好的了</span><wbr style="line-height:25px">,<br style="line-height:25px"> 但是有的朋友在G1中测试它还是有100ms左右的延迟,这可能会影响用户体验。<br style="line-height:25px"> 也许这不能管SoundPool本身,因为到了性能比较好的Droid中这个延迟就可以让人接受了。<br style="line-height:25px"> 在现阶段SoundPool有这些缺陷,但也有着它不可替代的优点,基于这些我们建议大在如下情况中多使用SoundPool:<br style="line-height:25px"><span style="color:#000080; line-height:25px">1.应用程序中的声效(按键提示音,消息等)<br style="line-height:25px"> 2.游戏中密集而短暂的声音(如多个飞船同时爆炸),当然大家还可以想到更多的使用方法。</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
使用SoundPool放声音
最新推荐文章于 2020-11-10 14:34:40 发布