欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android中怎么利用SoundPool播放音效-創(chuàng)新互聯(lián)

Android中怎么利用SoundPool播放音效,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

十多年專注建站、設(shè)計(jì)、互聯(lián)網(wǎng)產(chǎn)品定制網(wǎng)站制作服務(wù),業(yè)務(wù)涵蓋品牌網(wǎng)站設(shè)計(jì)、商城網(wǎng)站定制開發(fā)成都小程序開發(fā)、軟件系統(tǒng)開發(fā)、重慶APP開發(fā)等。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解每個(gè)客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)建站更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù),以推動(dòng)各中小企業(yè)全面信息數(shù)字化,并利用創(chuàng)新技術(shù)幫助各行業(yè)提升企業(yè)形象和運(yùn)營(yíng)效率。

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>來(lái)管理所加載的聲音; Step4:調(diào)用SoundPool的play()方法播放聲音。

下面的Demo程序示范了如何使用SoundPool來(lái)播放音效,該程序提供三個(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來(lái)管理這些音頻流    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來(lái)播放指定的聲音。這就是使用SoundPool播放聲音的標(biāo)準(zhǔn)過程。

實(shí)際使用SoundPool播放聲音時(shí)有如下幾點(diǎn)需要注意:SoundPool雖然可以一次性加載多個(gè)聲音,但由于內(nèi)存限制,因此應(yīng)該避免使用SoundPool來(lái)播放歌曲,只有那些短促、密集的聲音才考慮使用SoundPool進(jìn)行播放。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

本文題目:Android中怎么利用SoundPool播放音效-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://chinadenli.net/article24/dghcje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站改版營(yíng)銷型網(wǎng)站建設(shè)、建站公司響應(yīng)式網(wǎng)站、商城網(wǎng)站

廣告

聲明:本網(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)

商城網(wǎng)站建設(shè)