Android多媒體應(yīng)用使用SoundPool播放音頻

由于MediaPlayer占用資源較多,且不支持同時(shí)播放多個(gè)音頻,所以Android還提供了另一個(gè)播放音頻的類-----SoundPool。SoundPool即音頻池,可以同時(shí)播放多個(gè)短小的音頻,而且占用的資源較少。SoundPool適合在應(yīng)用程序中播放按鍵音或消息提示音等,在游戲中播放密集而短暫的聲音,如多個(gè)飛機(jī)爆炸的聲音等。使用SoundPool播放音頻,首先需要?jiǎng)?chuàng)建SoundPool對(duì)象,然后加載所需要播放的音頻,最后調(diào)用play()方法播放音頻,下面進(jìn)行詳細(xì)介紹

城陽(yáng)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司從2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司

1.創(chuàng)建SoundPool對(duì)象

SoundPool類提供了一個(gè)構(gòu)造方法,用來(lái)創(chuàng)建SoundPool對(duì)象,該構(gòu)造方法的語(yǔ)法格式如下:
SoundPool(int maxStreams,int streamType,int srcQuality);
其中,參數(shù)maxStreams用于指定可以容納多少個(gè)音頻;參數(shù)streamType用于指定聲音類型,可以通過(guò)AudioManager類提供的常量進(jìn)行指定,通常使用STREAM_MUSIC;參數(shù)srcQuality用于指定音頻的品質(zhì),默認(rèn)為0。

例如,創(chuàng)建可以容納10個(gè)音頻的SoundPool對(duì)象,可以使用下面的代碼:
SoundPool soundpool=new SoundPool(10,AudioManager.STREAM_MUSIC,0);

2.加載所要放的音頻

可以用load()方法來(lái)加載要播放的音頻。load()方法的語(yǔ)法格式有以下4種:
a.public int load(Context context,int resid,int priority);用于通過(guò)指定的資源ID來(lái)加載音頻
b.public int load(String path,int priority);用于通過(guò)音頻文件的路徑來(lái)加載音頻
c.public int load(AssetFileDescriptor afd,int priority);用于從AssetFileDescriptor所對(duì)應(yīng)的文件中加載音頻
d.public int load(FileDescriptor fd,long offset,long length,int priority);用于加載FileDescriptor對(duì)象中從offset開(kāi)始,長(zhǎng)度為length的音頻

例如,要通過(guò)資源ID來(lái)加載音頻文件ding.wav,可以使用下面的代碼:
soundpool.load(this,R.raw.ding,1);

3.播放音頻

調(diào)用SoundPool對(duì)象的play()方法可以播放指定的音頻。play()方法的語(yǔ)法格式如下:
play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate);
各個(gè)參數(shù)說(shuō)明如下:
soundID:用于指定要播放的音頻,該音頻為通過(guò)load()方法返回的音頻
leftVolume:用于指定左聲道的音量,取值范圍為0.0-1.0
rightVolume:用于指定右聲道的音量,取值范圍為0.0-1.0
priority:用于指定播放音頻的優(yōu)先級(jí),數(shù)值越大,優(yōu)先級(jí)越高
loop:用于指定循環(huán)次數(shù),0為不循環(huán),-1為循環(huán)
rate:用于指定速率,正常為1,最低為0.5,最高為2

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

下面寫(xiě)一個(gè)小實(shí)例,實(shí)現(xiàn)通過(guò)SoundPool播放音頻:
音頻文件放入位置如圖-10.12.a.jpg
布局文件,實(shí)現(xiàn)四個(gè)按鈕("狗叫"按鈕,"鳥(niǎo)叫"按鈕,"鬧鈴聲"按鈕,"笑聲"按鈕)
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="鳥(niǎo)叫"/> 
  <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;//聲明一個(gè)SoundPool對(duì)象 
 private HashMap<Integer,Integer> soundmap=new HashMap<Integer,Integer>();//創(chuàng)建一個(gè)HashMap對(duì)象 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
   
  //創(chuàng)建一個(gè)SoundPool對(duì)象,該對(duì)象可以容納5個(gè)音頻流 
  soundpool=new SoundPool(5,AudioManager.STREAM_MUSIC,0); 
   
  //將要播放的音頻流保存到HashMap對(duì)象中 
  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); 
     
   } 
  }); 
 
 
 } 
 //重寫(xiě)鍵盤(pán)被按下的onKeyDown()方法,用于實(shí)現(xiàn)播放按鍵音的功能 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) { 
  soundpool.play(soundmap.get(5), 1,1,0,0,1);//播放按鍵音 
  return true; 
 } 
} 

運(yùn)行結(jié)果如圖

Android多媒體應(yīng)用使用SoundPool播放音頻

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享文章:Android多媒體應(yīng)用使用SoundPool播放音頻
網(wǎng)頁(yè)URL:http://bm7419.com/article30/jddsso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站標(biāo)簽優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管