国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android多媒體應用使用SoundPool播放音頻

2019-12-12 01:23:57
字體:
來源:轉載
供稿:網友

由于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;  } } 

運行結果如圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屯昌县| 泗水县| 朝阳县| 正镶白旗| 东平县| 景宁| 贡山| 泾源县| 莲花县| 金山区| 社旗县| 冷水江市| 泸西县| 安塞县| 大竹县| 松潘县| 孝义市| 资源县| 霸州市| 黄冈市| 台南县| 蒙阴县| 聂拉木县| 荆州市| 香港 | 平利县| 阿鲁科尔沁旗| 额济纳旗| 伊金霍洛旗| 红桥区| 巴东县| 长海县| 桦甸市| 西青区| 夏河县| 凤台县| 察哈| 九龙坡区| 玛纳斯县| 依安县| 依安县|