国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用SoundPool播放音效

2019-12-12 00:42:13
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android使用SoundPool播放音效的具體代碼,供大家參考,具體內容如下

SoundPool(int maxStreams, int streamType, int srcQuality) 參數依次是:

①指定支持多少個聲音,SoundPool對象中允許同時存在的最大流的數量。
②指定聲音類型,流類型可以分為STREAM_VOICE_CALL(通話), STREAM_SYSTEM(系統), STREAM_RING(鈴聲),STREAM_MUSIC(媒體音量) 和STREAM_ALARM(警報)四種類型。在AudioManager中定義。
③指定聲音品質(采樣率變換質量),一般直接設置為0!、

以下是對它的常用方法的介紹:

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)

參數介紹:

  • context:上下文
  • resId:資源id
  • priority:沒什么用的一個參數,建議設置為1,保持和未來的兼容性
  • path:文件路徑
  • FileDescriptor:貌似是流吧,這個我也不知道
  • AssetFileDescriptor:從asset目錄讀取某個資源文件,其用法:AssetFileDescriptor descriptor = assetManager.openFd("biaobiao.mp3");

2.播放控制

play(int soundID,float leftVolume,float rightVolume,int priority,int loop,float rate)

參數依次是:

  • soundID:Load()返回的聲音ID號
  • leftVolume:左聲道音量設置
  • rightVolume:右聲道音量設置
  • priority:指定播放聲音的優先級,數值越高,優先級越大。
  • loop:指定是否循環:-1表示無限循環,0表示不循環,其他值表示要重復播放的次數
  • rate:指定播放速率:1.0的播放率可以使聲音按照其原始頻率,而2.0的播放速率,可以使聲音按照其 原始頻率的兩倍播放。如果為0.5的播放率,則播放速率是原始頻率的一半。播放速率的取值范圍是0.5至2.0。

3.資源釋放

方法:可以通過release()方法釋放所有SoundPool對象所占據的內存和資源,也可以根據聲音ID來釋放。

下面是使用SoundPool實現的一個代碼示例:

1.  運行效果圖:

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{  //設置最多可容納5個音頻流,音頻的品質為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>

點擊聲音1~5按鈕會發出聲音,但當點擊最后一個release按鈕將SoundPool釋放后,再去按就沒有任何效果了哦。

源碼下載:Android使用SoundPool播放音效

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通海县| 墨竹工卡县| 鸡西市| 老河口市| 遵义县| 遂川县| 赫章县| 金平| 台前县| 灵璧县| 鱼台县| 射洪县| 广安市| 保康县| 山东省| 古蔺县| 镇雄县| 南陵县| 锡林浩特市| 泗水县| 页游| 安西县| 宜宾市| 泽州县| 凯里市| 务川| 贵德县| 思南县| 盐山县| 长葛市| 呼伦贝尔市| 抚远县| 张家川| 凤凰县| 甘德县| 宁安市| 乐亭县| 健康| 府谷县| 沙坪坝区| 孟州市|