SoundPool类播放音频、案例弹钢琴
作者:互联网
SoundPool即音频池,可以同时播放多个短小的音频,而且占用资源少,其适合在应用程序中播放按键音或者消息提示音等。
SoundPool类常用方法
方法名称 | 功能描述 |
---|---|
load() | 加载音频文件 |
play() | 播放音频 |
pause(int streamID) | 根据加载的资源ID,暂停播放音频 |
resume(int streamID) | 根据加载的资源ID,继续播放暂停的音频资源 |
stop(int streamID) | 根据加载的资源ID,停止音频资源的播放 |
unload(int streamID) | 从音频池中卸载音频资源ID为soundID资源 |
release() | 释放音频池资源 |
通过SoundPool类播放音频
- 创建SoundPool对象
maxStreams:指定可以容纳多少个音频
streamType:用于指定音频类型(AudioManager.STREAM_MUSIC等)
srcQuality:用于指定音频的品质,默认为0
public SoundPool (int maxStreams,int streamType,int srcQuality)
//创建一个可以容纳10个音频的SoundPool对象
SoundPool soundpool=new SoundPool(10,AudioManager.STREAM_MUSIC,0);
- 加载音频文件
创建Soundpool对象后,接着调用load()方法来加载音频文件
通过指定资源ID来加载音频文件
public int load(Context context,int resId,int priority)
//通过资源ID加载音频文件alarm.wav
soundPool.load(this,R.id.raw.alarm,1);
- 播放音频
调用SoundPool对象paly()方法可播放指定的音频
play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)
参数介绍
参数 | 描述 |
---|---|
soundID | 指定要播放的音频ID,该音频是通过load()方法返回的音频 |
leftVolume/rightVolume | 指定左声道/右声道的音量,取值范围是0.0~1.0 |
priority | 优先级,数值越大,优先级越高 |
loop | 循环播放的次数,0表示不循环,-1表示循环 |
rate | 指定播放速率,1表示正常播放速率,0.5表示最低播放速率,2表示最高播放速率 |
播放raw文件夹下的sound.wav音频文件
soundPool.play(soundPool.load(MainActivity.this,R.raw.sound,1),1,1,0,0,1);
案例 — 弹钢琴
界面xml,使用LinearLayout布局,设置每个ImageView的权重为1,因此需要设置每个控件的宽度为0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity"
android:gravity="bottom">
<ImageView
android:id="@+id/iv1"
android:src="@drawable/icon_do_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv2"
android:src="@drawable/icon_re_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv3"
android:src="@drawable/icon_mi_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv4"
android:src="@drawable/icon_fa_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv5"
android:src="@drawable/icon_so_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv6"
android:src="@drawable/icon_la_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
<ImageView
android:id="@+id/iv7"
android:src="@drawable/icon_si_selector"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="250dp"
></ImageView>
</LinearLayout>
创建背景选择器,由于钢琴的每个键按下和弹起的背景不同,需要创建一个背景选择器来实现这个效果
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_do" android:state_pressed="false"/>
<item android:drawable="@drawable/icon_do_pressed" android:state_pressed="true"/>
</selector>
设置钢琴界面横屏显示,在AndroidMandifest.xml文件中找到对应的Activity添加标签
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
</activity>
编写界面交互代码,通过SoundPool类的play()方法实现播放每个钢琴按键的音乐功能
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView iv1,iv2,iv3,iv4,iv5,iv6,iv7;
private SoundPool soundPool;
private Map<Integer,Integer> mapID=new HashMap<Integer, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initSoundPool();
}
@Override
public void onClick(View v) {
play(v.getId());
}
private void play(int id) {
//播放音乐
soundPool.play(mapID.get(id),1,1,0,0,1);
}
//app关闭时需要将播放的资源进行释放
@Override
protected void onDestroy() {
super.onDestroy();
if (soundPool!=null){
soundPool.autoPause(); //暂停播放
soundPool.release(); //释放资源
soundPool=null;
}
}
private void init() {
iv1=findViewById(R.id.iv1);
iv2=findViewById(R.id.iv2);
iv3=findViewById(R.id.iv3);
iv4=findViewById(R.id.iv4);
iv5=findViewById(R.id.iv5);
iv6=findViewById(R.id.iv6);
iv7=findViewById(R.id.iv7);
iv1.setOnClickListener(this);
iv2.setOnClickListener(this);
iv3.setOnClickListener(this);
iv4.setOnClickListener(this);
iv5.setOnClickListener(this);
iv6.setOnClickListener(this);
iv7.setOnClickListener(this);
}
private void initSoundPool() {
//创建音频值对象
if (soundPool==null){
soundPool=new SoundPool(10, AudioManager.STREAM_MUSIC,0);
}
//用一个map对象来存储要点击的图片和对应的音乐文件ID的对应关系
mapID.put(R.id.iv1,soundPool.load(MainActivity.this,R.raw.music_do,1));
mapID.put(R.id.iv2,soundPool.load(MainActivity.this,R.raw.music_re,1));
mapID.put(R.id.iv3,soundPool.load(MainActivity.this,R.raw.music_mi,1));
mapID.put(R.id.iv4,soundPool.load(MainActivity.this,R.raw.music_fa,1));
mapID.put(R.id.iv5,soundPool.load(MainActivity.this,R.raw.music_so,1));
mapID.put(R.id.iv6,soundPool.load(MainActivity.this,R.raw.music_la,1));
mapID.put(R.id.iv7,soundPool.load(MainActivity.this,R.raw.music_si,1));
}
}
标签:load,int,音频,弹钢琴,soundPool,SoundPool,播放,id 来源: https://blog.csdn.net/weixin_43916678/article/details/117881789