Android使用SoundPool實(shí)現(xiàn)播放音效
如果在程序應(yīng)用中(比如:游戲的音效等)需要播放密集、短促的音效,這時(shí)就使用SoundPool來播放音效,SoundPool使用音效池的概念來管理多個(gè)短促的音效,例如它可以開始就10個(gè)音效,以后在程序中按音效的ID進(jìn)行播放。
SoundPool主要用于播放一些較短的聲音片段,與MediaPlayer相比,SoundPool的優(yōu)勢在 于CPU資源占用量低和反應(yīng)延遲小。另外,SoundPool還支持自行設(shè)置聲音的品質(zhì)、音量、播放比率等參數(shù)。
一般使用SoundPool播放聲音的步驟如下:
Step1:調(diào)用SoundPool.Builder的構(gòu)造器創(chuàng)建SoundPool.Builder對象,并可通過該Builder對象為SoundPool設(shè)置屬性;
Step2:調(diào)用SoundPool的構(gòu)造器創(chuàng)建SoundPool對象;
Step3:調(diào)用SoundPool對象的load()方法從指定資源、文件中加載聲音。最好使用HashMap< Integer, Integer>來管理所加載的聲音;
Step4:調(diào)用SoundPool的play()方法播放聲音。
下面的Demo程序示范了如何使用SoundPool來播放音效,該程序提供三個(gè)按鈕,分別用于播放不同的聲音。
layout/activity_main.xml界面代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/bomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爆炸聲" />
<Button
android:id="@+id/shot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="射擊聲" />
<Button
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="射箭聲" />
</LinearLayout>
MainActivity.java邏輯代碼如下:
package com.fukaimei.soundpooltest;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bomb, shot, arrow;
// 定義一個(gè)SoundPool
SoundPool soundPool;
HashMap<Integer, Integer> soundMap = new HashMap<>();
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bomb = (Button) findViewById(R.id.bomb);
shot = (Button) findViewById(R.id.shot);
arrow = (Button) findViewById(R.id.arrow);
AudioAttributes attr = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME) // 設(shè)置音效使用場景
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build(); // 設(shè)置音效的類型
soundPool = new SoundPool.Builder().setAudioAttributes(attr) // 設(shè)置音效池的屬性
.setMaxStreams(10) // 設(shè)置最多可容納10個(gè)音頻流
.build(); // ①
// load方法加載指定音頻文件,并返回所加載的音效ID
// 此處使用HashMap來管理這些音頻流
soundMap.put(1, soundPool.load(this, R.raw.bomb, 1)); // ②
soundMap.put(2, soundPool.load(this, R.raw.shot, 1)); // ②
soundMap.put(3, soundPool.load(this, R.raw.arrow, 1)); // ②
bomb.setOnClickListener(this);
shot.setOnClickListener(this);
arrow.setOnClickListener(this);
}
// 重寫OnClickListener監(jiān)聽器接口的方法
@Override
public void onClick(View v) {
// 判斷哪個(gè)按鈕被單擊
if (v.getId() == R.id.bomb) {
soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1); // ③
} else if (v.getId() == R.id.shot) {
soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1); // ③
} else if (v.getId() == R.id.arrow) {
soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1); // ③
}
}
}
上面Demo程序代碼中標(biāo)①的代碼用于創(chuàng)建SoundPool對象;標(biāo)②的代碼用于使用SoundPool加載多個(gè)不同的聲音;標(biāo)③的代碼則用于根據(jù)聲音ID來播放指定的聲音。這就是使用SoundPool播放聲音的標(biāo)準(zhǔn)過程。
實(shí)際使用SoundPool播放聲音時(shí)有如下幾點(diǎn)需要注意:SoundPool雖然可以一次性加載多個(gè)聲音,但由于內(nèi)存限制,因此應(yīng)該避免使用SoundPool來播放歌曲,只有那些短促、密集的聲音才考慮使用SoundPool進(jìn)行播放。
Demo程序運(yùn)行效果界面截圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:使用SurfaceView實(shí)現(xiàn)視頻彈幕
欄 目:Android
下一篇:Android自定義View實(shí)現(xiàn)彈幕效果
本文標(biāo)題:Android使用SoundPool實(shí)現(xiàn)播放音效
本文地址:http://www.jygsgssxh.com/a1/Android/9036.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?


