Android使用SoundPool播放音效
本文實(shí)例為大家分享了Android使用SoundPool播放音效的具體代碼,供大家參考,具體內(nèi)容如下
SoundPool(int maxStreams, int streamType, int srcQuality) 參數(shù)依次是:
①指定支持多少個(gè)聲音,SoundPool對(duì)象中允許同時(shí)存在的最大流的數(shù)量。
②指定聲音類型,流類型可以分為STREAM_VOICE_CALL(通話), STREAM_SYSTEM(系統(tǒng)), STREAM_RING(鈴聲),STREAM_MUSIC(媒體音量) 和STREAM_ALARM(警報(bào))四種類型。在AudioManager中定義。
③指定聲音品質(zhì)(采樣率變換質(zhì)量),一般直接設(shè)置為0!、
以下是對(duì)它的常用方法的介紹:
1.加載聲音資源
load(Context context,int resid,int priority)
load(String path,int priority)
load(FileDescriptor fd,long offset,long length,int priority)
load(AssetFileDescriptor afd,int priority)
參數(shù)介紹:
- context:上下文
- resId:資源id
- priority:沒(méi)什么用的一個(gè)參數(shù),建議設(shè)置為1,保持和未來(lái)的兼容性
- path:文件路徑
- FileDescriptor:貌似是流吧,這個(gè)我也不知道
- AssetFileDescriptor:從asset目錄讀取某個(gè)資源文件,其用法:AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3");
2.播放控制
play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)
參數(shù)依次是:
- soundID:Load()返回的聲音ID號(hào)
- leftVolume:左聲道音量設(shè)置
- rightVolume:右聲道音量設(shè)置
- priority:指定播放聲音的優(yōu)先級(jí),數(shù)值越高,優(yōu)先級(jí)越大。
- loop:指定是否循環(huán):-1表示無(wú)限循環(huán),0表示不循環(huán),其他值表示要重復(fù)播放的次數(shù)
- rate:指定播放速率:1.0的播放率可以使聲音按照其原始頻率,而2.0的播放速率,可以使聲音按照其 原始頻率的兩倍播放。如果為0.5的播放率,則播放速率是原始頻率的一半。播放速率的取值范圍是0.5至2.0。
3.資源釋放
方法:可以通過(guò)release()方法釋放所有SoundPool對(duì)象所占據(jù)的內(nèi)存和資源,也可以根據(jù)聲音ID來(lái)釋放。
下面是使用SoundPool實(shí)現(xiàn)的一個(gè)代碼示例:
1. 運(yùn)行效果圖:
2. MainActivity代碼:
import android.content.res.AssetManager; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import java.util.HashMap; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btnOne; private Button btnTwo; private Button btnThree; private Button btnFour; private Button btnFive; private Button btn_release; private AssetManager aManager; private SoundPool mSoundPool = null; private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); aManager = getAssets(); try { initSP(); } catch (Exception e) { e.printStackTrace(); } bindViews(); } private void bindViews() { btnOne = (Button) findViewById(R.id.btn_play1); btnTwo = (Button) findViewById(R.id.btn_play2); btnThree = (Button) findViewById(R.id.btn_play3); btnFour = (Button) findViewById(R.id.btn_play4); btnFive = (Button) findViewById(R.id.btn_play5); btn_release = (Button) findViewById(R.id.btn_release); btnOne.setOnClickListener(this); btnTwo.setOnClickListener(this); btnThree.setOnClickListener(this); btnFour.setOnClickListener(this); btnFive.setOnClickListener(this); btn_release.setOnClickListener(this); } private void initSP() throws Exception{ //設(shè)置最多可容納5個(gè)音頻流,音頻的品質(zhì)為5 mSoundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 5); soundID.put(1, mSoundPool.load(this, R.raw.duang, 1)); soundID.put(2 , mSoundPool.load(getAssets().openFd("biaobiao.mp3") , 1)); //需要捕獲IO異常 soundID.put(3, mSoundPool.load(this, R.raw.duang, 1)); soundID.put(4, mSoundPool.load(this, R.raw.duang, 1)); soundID.put(5, mSoundPool.load(this, R.raw.duang, 1)); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_play1: mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1); break; case R.id.btn_play2: mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1); break; case R.id.btn_play3: mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1); break; case R.id.btn_play4: mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1); break; case R.id.btn_play5: mSoundPool.play(soundID.get(5), 1, 1, 0, 0, 1); break; case R.id.btn_release: mSoundPool.release(); //回收SoundPool資源 break; } } }
3. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_play1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聲音1" /> <Button android:id="@+id/btn_play2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聲音2" /> <Button android:id="@+id/btn_play3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聲音3" /> <Button android:id="@+id/btn_play4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聲音4" /> <Button android:id="@+id/btn_play5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聲音5" /> <Button android:id="@+id/btn_release" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="釋放SoundPool" /> </LinearLayout>
點(diǎn)擊聲音1~5按鈕會(huì)發(fā)出聲音,但當(dāng)點(diǎn)擊最后一個(gè)release按鈕將SoundPool釋放后,再去按就沒(méi)有任何效果了哦。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SoundPool實(shí)現(xiàn)簡(jiǎn)短小音效
- Android使用SoundPool播放短音效
- Android使用SoundPool實(shí)現(xiàn)播放音效
- Android使用SoundPool播放音效實(shí)例
- Android使用SoundPool實(shí)現(xiàn)播放音頻
- Android中SoundPool的使用步驟實(shí)例
- Android多媒體應(yīng)用使用SoundPool播放音頻
- android使用SoundPool播放音效的方法
- Android編程實(shí)現(xiàn)使用SoundPool播放音樂(lè)的方法
- Android利用SoundPool實(shí)現(xiàn)音樂(lè)池
相關(guān)文章
android浮層圖片拖動(dòng)并且可點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了android浮層的圖片拖動(dòng)并且可點(diǎn)擊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12fragment中的add和replace方法的區(qū)別淺析
使用 FragmentTransaction 的時(shí)候,它提供了這樣兩個(gè)方法,一個(gè) add , 一個(gè) replace ,對(duì)這兩個(gè)方法的區(qū)別一直有點(diǎn)疑惑。下面小編通過(guò)本文給大家簡(jiǎn)單介紹下fragment中的add和replace方法的區(qū)別,一起看看吧2017-01-01linphone-sdk-android版本號(hào)生成解析
這篇文章主要為大家介紹了linphone-sdk-android版本號(hào)生成解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09EasyValidate優(yōu)雅地校驗(yàn)提交數(shù)據(jù)完整性
這篇文章主要介紹了EasyValidate優(yōu)雅地校驗(yàn)提交數(shù)據(jù)完整性,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Android 簡(jiǎn)單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
這篇文章主要介紹了Android 簡(jiǎn)單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03Android導(dǎo)入現(xiàn)有的數(shù)據(jù)庫(kù)方法示例
這篇文章主要介紹了Android導(dǎo)入現(xiàn)有的數(shù)據(jù)庫(kù)方法,文中通過(guò)示例代碼介紹的很詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-02-02Flutter Http網(wǎng)絡(luò)請(qǐng)求實(shí)現(xiàn)詳解
這篇文章主要介紹了Flutter Http網(wǎng)絡(luò)請(qǐng)求實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置
這篇文章主要介紹了Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android Studio 修改應(yīng)用包名實(shí)例詳解
這篇文章主要介紹了Android Studio 修改應(yīng)用包名實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03