介绍
怎么在Android中使用SoundPool播放音效?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
SoundPool主要用于播放一些较短的声音片段,与媒体播放器相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质,音量,播放比率等参数。
一般使用SoundPool播放声音的步骤如下:
步骤1:调用SoundPool.Builder的构造器创建SoundPool。建筑商对象,并可通过该建筑商对象为SoundPool设置属性;
步骤2:调用SoundPool的构造器创建SoundPool对象,
步骤3:调用SoundPool对象的负载()方法从指定资源,文件中加载声音。最好使用HashMap<整数,Integer>来管理所加载的声音;
目的:调用SoundPool的发挥()方法播放声音。
下面的演示程序示范了如何使用SoundPool来播放音效,该程序提供三个按钮,分别用于播放不同的声音。
/activity_main布局。xml界面代码如下:
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" android:才能layout_width=癿atch_parent" android:才能layout_height=癿atch_parent" android:才能取向=癶orizontal"比; & lt;才能按钮 ,,,android: id=癅 + id/bomb" ,,,android: layout_width=皐rap_content" ,,,android: layout_height=皐rap_content" ,,,android:文本=氨ㄉ?/比; & lt;才能按钮 ,,,android: id=癅 + id/shot" ,,,android: layout_width=皐rap_content" ,,,android: layout_height=皐rap_content" ,,,android:文本=吧浠魃?/比; & lt;才能按钮 ,,,android: id=癅 + id/arrow" ,,,android: layout_width=皐rap_content" ,,,android: layout_height=皐rap_content" ,,,android:文本=吧浼?/比; & lt;/LinearLayout>
MainActivity。java逻辑代码如下:
package com.fukaimei.soundpooltest; import android.media.AudioAttributes; import android.media.SoundPool; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.util.HashMap; public class  MainActivity extends AppCompatActivity  implements View.OnClickListener { Button 炸弹,才能,,,箭头;//,才能定义一个SoundPool SoundPool 才能;soundPool; ,,HashMap<整数,Integer>, soundMap =, new HashMap<在(); @RequiresApi才能(api =, Build.VERSION_CODES.LOLLIPOP) @Override才能 protected 才能;void  onCreate (Bundle savedInstanceState), { ,,,super.onCreate (savedInstanceState); ,,,setContentView (R.layout.activity_main); ,,,bomb =,(按钮),findViewById (R.id.bomb); ,,,shot =,(按钮),findViewById (R.id.shot); ,,,arrow =,(按钮),findViewById (R.id.arrow); ,,,AudioAttributes attr =, new AudioAttributes.Builder () .setUsage (AudioAttributes.USAGE_GAME),//,设置音效使用场景 ,,,,,,,.setContentType (AudioAttributes.CONTENT_TYPE_MUSIC) .build();,//,设置音效的类型 ,,,soundPool =, new SoundPool.Builder () .setAudioAttributes (attr),//,设置音效池的属性 ,,,,,,,.setMaxStreams(10),//,设置最多可容纳10个音频流 ,,,,,,,.build ();,//① ,,,//,负载方法加载指定音频文件,并返回所加载的音效ID ,,,//,此处使用HashMap来管理这些音频流 ,,,soundMap.put (1, soundPool.load (R.raw.bomb,,, 1)),,//② ,,,soundMap.put (2, soundPool.load (R.raw.shot,,, 1)),,//② ,,,soundMap.put (3, soundPool.load (R.raw.arrow,,, 1)),,//② ,,,bomb.setOnClickListener(这个); ,,,shot.setOnClickListener(这个); ,,,arrow.setOnClickListener(这个); ,,}//,才能重写OnClickListener监听器接口的方法 @Override才能 public 才能;void  onClick (View v), { ,,,//,判断哪个按钮被单击 ,,,if (v.getId (),==, R.id.bomb), { ,,,,,soundPool.play (soundMap.get (1),, 1,, 1,, 0, 0, 1),,//③ ,,,},else if (v.getId (),==, R.id.shot), { ,,,,,soundPool.play (soundMap.get (2),, 1,, 1,, 0, 0, 1),,//③ null null null null null怎么在Android中使用SoundPool播放音效