乐读文学

Android从入门到精通

乐读文学 > 科普学习 > Android从入门到精通

第116页

书籍名:《Android从入门到精通》    作者:明日科技


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_SYSTEM,  0);  //创建一个SoundPool对象,该对象可以容纳10个音频流

2.  加载所要播放的音频

创建SoundPool对象后,可以调用load()方法来加载要播放的音频。load()方法的语法格式有以下4种。

[√]public  int  load  (Context  context,  int  resId,  int  priority):用于通过指定的资源ID来加载音频。



[√]public  int  load  (String  path,  int  priority):用于通过音频文件的路径来加载音频。



[√]public  int  load  (AssetFileDescriptor  afd,  int  priority):用于从AssetFileDescriptor所对应的文件中加载音频。



[√]public  int  load  (FileDescriptor  fd,  long  offset,  long  length,  int  priority):用于加载FileDescriptor对象中从offset开始,长度为length的音频。

例如,要通过资源ID来加载音频文件ding.wav,可以使用下面的代码:

soundpool.load(this,  R.raw.ding  ,  1);

说明:  为了更好地管理所加载的每个音频,一般使用HashMap对象来管理这些音频。这时可以先创建一个HashMap对象,然后应用该对象的put()方法将加载的音频保存到该对象中。例如,创建一个HashMap对象,并应用put()方法添加一个音频,可以使用下面的代码:

HashMap  soundmap  =  new  HashMap();  //创建一个HashMap对象



soundmap.put(1,  soundpool.load(this,  R.raw.chimes,  1));

3.  播放音频

调用SoundPool对象的play()方法可播放指定的音频。play()方法的语法格式如下:

play  (int  soundID,  float  leftVolume,  float  rightVolume,  int  priority,  int  loop,  float  rate)

play()方法各参数的说明如表10.1所示。

表10.1 play()方法的参数说明





参 数  描 述

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(MainActivity.this,  R.raw.notify,  1),  1,  1,  0,  0,  1);  //播放指定的音频

例10.2   在Eclipse中创建Android项目,名称为10.2,实现通过SoundPool播放音频。(实例位置:光盘\TM\sl\10\10.2)

(1)修改新建项目的res\layout目录下的布局文件main.xml,将默认添加的TextView组件删除,然后在默认添加的线性布局管理器中添加4个按钮组件,分别为“风铃声”按钮、“布谷鸟叫声”按钮、“门铃声”按钮和“电话声”按钮,具体代码请参见光盘。

(2)打开默认添加的MainActivity,在该类中,创建两个成员变量,具体代码如下:

private  SoundPool  soundpool;  //声明一个SoundPool对象



private  HashMap  soundmap  =  new  HashMap();  //创建一个HashMap对象

(3)在onCreate()方法中,首先获取布局管理器中添加的“风铃声”按钮、“布谷鸟叫声”按钮、“门铃声”按钮和“电话声”按钮,然后实例化SoundPool对象,再将要播放的全部音频流保存到HashMap对象中,具体代码如下:

Button  chimes  =  (Button)  findViewById(R.id.button1);  //获取“风铃声”按钮



Button  enter  =  (Button)  findViewById(R.id.button2);  //获取“布谷鸟叫声”按钮



Button  notify  =  (Button)  findViewById(R.id.button3);  //获取“门铃声”按钮



Button  ringout  =  (Button)  findViewById(R.id.button4);  //获取“电话声”按钮



soundpool  =  new  SoundPool(5,



AudioManager.STREAM_SYSTEM,  0);  //创建一个SoundPool对象,该对象可以容纳5个音频流



//将要播放的音频流保存到HashMap对象中



soundmap.put(1,  soundpool.load(this,  R.raw.chimes,  1));



soundmap.put(2,  soundpool.load(this,  R.raw.enter,  1));



soundmap.put(3,  soundpool.load(this,  R.raw.notify,  1));



soundmap.put(4,  soundpool.load(this,  R.raw.ringout,  1));



soundmap.put(5,  soundpool.load(this,  R.raw.ding,  1));