怎么在Android中使用SoundPool播放音效?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯專注于天等企業(yè)網站建設,成都響應式網站建設公司,成都做商城網站。天等網站建設公司,為天等等地區(qū)提供建站服務。全流程按需求定制開發(fā),專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯專業(yè)和態(tài)度為您提供的服務
SoundPool主要用于播放一些較短的聲音片段,與MediaPlayer相比,SoundPool的優(yōu)勢在 于CPU資源占用量低和反應延遲小。另外,SoundPool還支持自行設置聲音的品質、音量、播放比率等參數。
一般使用SoundPool播放聲音的步驟如下:
Step1:調用SoundPool.Builder的構造器創(chuàng)建SoundPool.Builder對象,并可通過該Builder對象為SoundPool設置屬性;
Step2:調用SoundPool的構造器創(chuàng)建SoundPool對象;
Step3:調用SoundPool對象的load()方法從指定資源、文件中加載聲音。最好使用HashMap< Integer, Integer>來管理所加載的聲音;
Step4:調用SoundPool的play()方法播放聲音。
下面的Demo程序示范了如何使用SoundPool來播放音效,該程序提供三個按鈕,分別用于播放不同的聲音。
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; // 定義一個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) // 設置音效使用場景 .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build(); // 設置音效的類型 soundPool = new SoundPool.Builder().setAudioAttributes(attr) // 設置音效池的屬性 .setMaxStreams(10) // 設置最多可容納10個音頻流 .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) { // 判斷哪個按鈕被單擊 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程序代碼中標①的代碼用于創(chuàng)建SoundPool對象;標②的代碼用于使用SoundPool加載多個不同的聲音;標③的代碼則用于根據聲音ID來播放指定的聲音。這就是使用SoundPool播放聲音的標準過程。
實際使用SoundPool播放聲音時有如下幾點需要注意:SoundPool雖然可以一次性加載多個聲音,但由于內存限制,因此應該避免使用SoundPool來播放歌曲,只有那些短促、密集的聲音才考慮使用SoundPool進行播放。
Demo程序運行效果界面截圖如下:
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯行業(yè)資訊頻道,感謝您對創(chuàng)新互聯的支持。
名稱欄目:怎么在Android中使用SoundPool播放音效
新聞來源:http://bm7419.com/article6/jjceig.html
成都網站建設公司_創(chuàng)新互聯,為您提供虛擬主機、網頁設計公司、品牌網站建設、營銷型網站建設、企業(yè)建站、做網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯