Android使用SoundPool實(shí)現(xiàn)播放音效
如果在程序應(yīng)用中(比如:游戲的音效等)需要播放密集、短促的音效,這時(shí)就使用SoundPool來播放音效,SoundPool使用音效池的概念來管理多個(gè)短促的音效,例如它可以開始就10個(gè)音效,以后在程序中按音效的ID進(jìn)行播放。
SoundPool主要用于播放一些較短的聲音片段,與MediaPlayer相比,SoundPool的優(yōu)勢(shì)在 于CPU資源占用量低和反應(yīng)延遲小。另外,SoundPool還支持自行設(shè)置聲音的品質(zhì)、音量、播放比率等參數(shù)。
一般使用SoundPool播放聲音的步驟如下:
Step1:調(diào)用SoundPool.Builder的構(gòu)造器創(chuàng)建SoundPool.Builder對(duì)象,并可通過該Builder對(duì)象為SoundPool設(shè)置屬性;
Step2:調(diào)用SoundPool的構(gòu)造器創(chuàng)建SoundPool對(duì)象;
Step3:調(diào)用SoundPool對(duì)象的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è)置音效使用場(chǎng)景 .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對(duì)象;標(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SoundPool實(shí)現(xiàn)簡(jiǎn)短小音效
- Android使用SoundPool播放短音效
- Android使用SoundPool播放音效實(shí)例
- Android使用SoundPool實(shí)現(xiàn)播放音頻
- Android中SoundPool的使用步驟實(shí)例
- Android使用SoundPool播放音效
- Android多媒體應(yīng)用使用SoundPool播放音頻
- android使用SoundPool播放音效的方法
- Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法
- Android利用SoundPool實(shí)現(xiàn)音樂池
相關(guān)文章
android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android 使用 Scroller 實(shí)現(xiàn)平滑滾動(dòng)功能的示例代碼
這篇文章主要介紹了Android 使用 Scroller 實(shí)現(xiàn)平滑滾動(dòng)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Flutter應(yīng)用框架搭建實(shí)現(xiàn)屏幕適配方案詳解
移動(dòng)設(shè)備多樣性,特別是Android的碎片化嚴(yán)重,存在各種各樣的分辨率,flutter跨平臺(tái)開發(fā)又需要同時(shí)支持Android和IOS,為盡可能的還原設(shè)計(jì)圖效果提升用戶的體驗(yàn),根據(jù)設(shè)計(jì)稿設(shè)計(jì)屏幕ui的時(shí)候我們需要考慮到屏幕適配的問題2022-11-11創(chuàng)建Android庫的方法及Android .aar文件用法小結(jié)
本文給大家介紹了創(chuàng)建Android庫的方法及Android中 .aar文件生成方法與用法詳解,涉及到創(chuàng)建庫模塊操作步驟及開發(fā)注意事項(xiàng),需要的朋友參考下吧2017-12-12android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼
這篇文章主要介紹了android 實(shí)現(xiàn)APP中改變頭像圖片的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07MPAndroidChart 自定義圖表繪制使用實(shí)例
這篇文章主要為大家介紹了MPAndroidChart 自定義圖表繪制使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android Jetpack 狠活Lifecycles與LiveData使用詳解
這篇文章主要為大家介紹了Android Jetpack 狠活Lifecycles與LiveData使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Android實(shí)現(xiàn)銀行卡、手機(jī)號(hào)帶空格格式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)銀行卡、手機(jī)號(hào)帶空格的格式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12