Android中SoundPool的使用步驟實(shí)例
大家知道MediaPlayer占用的資源比較多,且不可以同時(shí)支持播放多個(gè)音頻,所以我們有一種叫做SoundPool,比如我們常見的按鍵音或者是手機(jī)提示音,還比如我們?cè)谟螒虻拈_發(fā)中會(huì)有大量的音效效果等,下邊介紹一下她的用法:
步驟如下:
1.創(chuàng)建SoundPool對(duì)象
源碼如下
/** *SoundPool源碼中的構(gòu)造方法方法體 * @param maxStreams 最多可以容納多少個(gè)音頻 * @param streamType 指定的聲音類型,通過AudioManager類提供的常量進(jìn)行指定 * @param srcQuality 指定音頻的質(zhì)量,默認(rèn)為0 * @return a SoundPool object, or null if creation failed */ public SoundPool(int maxStreams, int streamType, int srcQuality)
2.加載所需要播放的音頻:
/** * @param context the application context * @param resId the resource ID * @param priority the priority of the sound. Currently has no effect. Use * a value of 1 for future compatibility. * @return a sound ID. This value can be used to play or unload the sound. */ public int load(Context context, int resId, int priority);
3.播放音頻
/** * Play a sound from a sound ID. * @param soundID 通過load方法返回的音頻 * @param leftVolume 左聲道的音量 * @param rightVolume 右聲道的音量 * @param priority 優(yōu)先級(jí),值越大,優(yōu)先級(jí)越高 * @param loop 循環(huán)的次數(shù):0為不循環(huán),-1為循環(huán) * @param rate 指定速率,正常位1,為地位0.5,最高位2 * @return non-zero streamID if successful, zero if failed */ public final int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);
4.案例如下:
(1)布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="風(fēng)鈴聲" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="布谷鳥叫聲" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="門鈴聲" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電話聲" /> </LinearLayout>
(2)MainActivity.java文件
package com.mingrisoft; 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ì)象 //使用HashMap管理各種音頻 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); Button chimes = (Button) findViewById(R.id.button1); //獲取“風(fēng)鈴聲”按鈕 Button enter = (Button) findViewById(R.id.button2); //獲取“布谷鳥叫聲”按鈕 Button notify = (Button) findViewById(R.id.button3); //獲取“門鈴聲”按鈕 Button ringout = (Button) findViewById(R.id.button4); //獲取“電話聲”按鈕 soundpool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 0); //創(chuàng)建一個(gè)SoundPool對(duì)象,該對(duì)象可以容納5個(gè)音頻流 //將要播放的音頻流保存到HashMap對(duì)象中 soundmap.put(1, soundpool.load(this, R.raw.chimes, 1)); soundmap.put(2, soundpool.load(this, R.raw.enter, 1)); soundmap.put(3, soundpool.load(this, R.raw.notify, 1)); soundmap.put(4, soundpool.load(this, R.raw.ringout, 1)); soundmap.put(5, soundpool.load(this, R.raw.ding, 1)); //為各按鈕添加單擊事件監(jiān)聽器 chimes.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1); //播放指定的音頻 } }); enter.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);//播放指定的音頻 } }); notify.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);//播放指定的音頻 } }); ringout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);//播放指定的音頻 soundpool.play(soundpool.load(MainActivity.this, R.raw.notify, 1), 1, 1, 0, 0, 1); } }); } //重寫鍵被按下的事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1); //播放按鍵音 return true; } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android SoundPool實(shí)現(xiàn)簡短小音效
- Android使用SoundPool播放短音效
- Android使用SoundPool實(shí)現(xiàn)播放音效
- Android使用SoundPool播放音效實(shí)例
- Android使用SoundPool實(shí)現(xiàn)播放音頻
- Android使用SoundPool播放音效
- Android多媒體應(yīng)用使用SoundPool播放音頻
- android使用SoundPool播放音效的方法
- Android編程實(shí)現(xiàn)使用SoundPool播放音樂的方法
- Android利用SoundPool實(shí)現(xiàn)音樂池
相關(guān)文章
Android保存的文件顯示到文件管理的最近文件和下載列表中的方法
這篇記錄的是Android中如何把我們往存儲(chǔ)中寫入的文件,如何顯示到文件管理的下載列表、最近文件列表中,需要的朋友可以參考下2020-01-01淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐
本篇文章主要介紹了淺談Android ASM自動(dòng)埋點(diǎn)方案實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
這篇文章主要介紹了Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下2014-09-09淺談Android Activity與Service的交互方式
下面小編就為大家?guī)硪黄獪\談Android Activity與Service的交互方式。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09Android RecyclerView下拉刷新和上拉加載更多
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView下拉刷新和上拉加載更多,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
這篇文章主要給大家介紹了關(guān)于Android如何將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法,文中通過示例代碼介紹的非常吸納關(guān)系,對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11關(guān)于Android bitmap你不知道的一些事
這篇文章主要為大家詳細(xì)介紹了關(guān)于Android bitmap你不知道的一些事,使用bitmap需要注意的一些細(xì)節(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android Studio 升級(jí)到3.0 提示 java.lang.NoClassDefFoundError的解決方法
這篇文章主要介紹了Android Studio 升級(jí)到3.0 提示 java.lang.NoClassDefFoundError的解決方法,需要的朋友可以參考下2017-12-12