且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Android开发】多媒体应用开发-使用SoundPool播放音频

更新时间:2022-06-03 21:20:36

由于MediaPlayer占用资源较多,且不支持同时播放多个音频,所以Android还提供了另一个播放音频的类-----SoundPool。SoundPool即音频池,可以同时播放多个短小的音频,而且占用的资源较少。SoundPool适合在应用程序中播放按键音或消息提示音等,在游戏中播放密集而短暂的声音,如多个飞机爆炸的声音等。使用SoundPool播放音频,首先需要创建SoundPool对象,然后加载所需要播放的音频,最后调用play()方法播放音频,下面进行详细介绍

1.创建SoundPool对象
SoundPool类提供了一个构造方法,用来创建SoundPool对象,该构造方法的语法格式如下:
SoundPool(int maxStreams,int streamType,int srcQuality);
其中,参数maxStreams用于指定可以容纳多少个音频;参数streamType用于指定声音类型,可以通过AudioManager类提供的常量进行指定,通常使用STREAM_MUSIC;参数srcQuality用于指定音频的品质,默认为0。

例如,创建可以容纳10个音频的SoundPool对象,可以使用下面的代码:
SoundPool soundpool=new SoundPool(10,AudioManager.STREAM_MUSIC,0);

2.加载所要放的音频
可以用load()方法来加载要播放的音频。load()方法的语法格式有以下4种:
a.public int load(Context context,int resid,int priority);用于通过指定的资源ID来加载音频
b.public int load(String path,int priority);用于通过音频文件的路径来加载音频
c.public int load(AssetFileDescriptor afd,int priority);用于从AssetFileDescriptor所对应的文件中加载音频
d.public int load(FileDescriptor fd,long offset,long length,int priority);用于加载FileDescriptor对象中从offset开始,长度为length的音频


例如,要通过资源ID来加载音频文件ding.wav,可以使用下面的代码:
soundpool.load(this,R.raw.ding,1);

3.播放音频
调用SoundPool对象的play()方法可以播放指定的音频。play()方法的语法格式如下:
play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate);
各个参数说明如下:
soundID:用于指定要播放的音频,该音频为通过load()方法返回的音频
leftVolume:用于指定左声道的音量,取值范围为0.0-1.0
rightVolume:用于指定右声道的音量,取值范围为0.0-1.0
priority:用于指定播放音频的优先级,数值越大,优先级越高
loop:用于指定循环次数,0为不循环,-1为循环
rate:用于指定速率,正常为1,最低为0.5,最高为2


例如,要播放音频资源中保存的音频文件notify.wav,可以使用下面的代码:
soundpool.play(soundpool.load(Manactivity.this,R.raw.notify,1),1,1,0,0,1);

下面写一个小实例,实现通过SoundPool播放音频:
音频文件放入位置如图-10.12.a.jpg
布局文件,实现四个按钮("狗叫"按钮,"鸟叫"按钮,"闹铃声"按钮,"笑声"按钮)
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    	<Button 
	        android:id="@+id/dog"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="狗叫"/>
	    <Button 
	        android:id="@+id/brid"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="鸟叫"/>
	    <Button 
	        android:id="@+id/notify"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="闹铃声"/>
	    <Button 
	        android:id="@+id/laugh"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="笑声"/>
</LinearLayout>

MainActivity:
package com.example.test;  
  
import java.util.HashMap;


import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
  
public class MainActivity extends Activity{  
	private SoundPool soundpool;//声明一个SoundPool对象
	private HashMap<Integer,Integer> soundmap=new HashMap<Integer,Integer>();//创建一个HashMap对象
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);
        
        //创建一个SoundPool对象,该对象可以容纳5个音频流
        soundpool=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
        
        //将要播放的音频流保存到HashMap对象中
        soundmap.put(1,soundpool.load(this, R.raw.dog,1));
        soundmap.put(2,soundpool.load(this, R.raw.brid,1));
        soundmap.put(3,soundpool.load(this, R.raw.notify,1));
        soundmap.put(4,soundpool.load(this, R.raw.laugh,1));
        soundmap.put(5,soundpool.load(this, R.raw.ding,1));
        
        Button dog=(Button)findViewById(R.id.dog);
        dog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				soundpool.play(soundmap.get(1), 1,1,0,0,1);
				
			}
		});
        
        Button brid=(Button)findViewById(R.id.brid);
        brid.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				soundpool.play(soundmap.get(2), 1,1,0,0,1);
				
			}
		});
        
        Button notify=(Button)findViewById(R.id.notify);
        notify.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				soundpool.play(soundmap.get(3), 1,1,0,0,1);
				
			}
		});
        
        Button laugh=(Button)findViewById(R.id.laugh);
        laugh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				soundpool.play(soundmap.get(4), 1,1,0,0,1);
				
			}
		});


    }
    //重写键盘被按下的onKeyDown()方法,用于实现播放按键音的功能
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		soundpool.play(soundmap.get(5), 1,1,0,0,1);//播放按键音
		return true;
	}
}  

运行结果如图

【Android开发】多媒体应用开发-使用SoundPool播放音频

转载请注明出处:http://blog.csdn.net/acmman/article/details/46551047