本文實(shí)例為大家分享了Android使用SoundPool播放音效的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)專注于襄汾企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開(kāi)發(fā)。襄汾網(wǎng)站建設(shè)公司,為襄汾等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
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ù)介紹:
2.播放控制
play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)
參數(shù)依次是:
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)有任何效果了哦。
源碼下載:Android使用SoundPool播放音效
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:Android使用SoundPool播放音效
網(wǎng)頁(yè)路徑:http://www.ekvhdxd.cn/article32/ihghsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站收錄、全網(wǎng)營(yíng)銷推廣、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)