最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用SoundPool播放音效

 更新時間:2018年07月18日 10:12:58   作者:dsd2333  
這篇文章主要為大家詳細介紹了Android使用SoundPool播放音效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

①指定支持多少個聲音,SoundPool對象中允許同時存在的最大流的數(shù)量。
②指定聲音類型,流類型可以分為STREAM_VOICE_CALL(通話), STREAM_SYSTEM(系統(tǒng)), STREAM_RING(鈴聲),STREAM_MUSIC(媒體音量) 和STREAM_ALARM(警報)四種類型。在AudioManager中定義。
③指定聲音品質(zhì)(采樣率變換質(zhì)量),一般直接設(shè)置為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)

參數(shù)介紹:

  • context:上下文
  • resId:資源id
  • priority:沒什么用的一個參數(shù),建議設(shè)置為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)

參數(shù)依次是:

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

3.資源釋放

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

下面是使用SoundPool實現(xiàn)的一個代碼示例:

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android利用Flutter?path繪制粽子的示例代碼

    Android利用Flutter?path繪制粽子的示例代碼

    端午將至,作為中華民族的非常重要的傳統(tǒng)節(jié)日,粽子那是必不可少的。今天跟隨本篇文章用Flutter?path畫一個會科普節(jié)日的的粽子吧
    2022-05-05
  • 解析libcurl在android下的移植、編譯與測試

    解析libcurl在android下的移植、編譯與測試

    本篇文章是對libcurl在android下的移植、編譯與測試進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • android 復(fù)制 粘貼 剪切功能應(yīng)用

    android 復(fù)制 粘貼 剪切功能應(yīng)用

    網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下
    2012-11-11
  • Android 頂部標題欄隨滑動時的漸變隱藏和漸變顯示效果

    Android 頂部標題欄隨滑動時的漸變隱藏和漸變顯示效果

    這篇文章主要介紹了Android 頂部標題欄隨滑動時的漸變隱藏和漸變顯示效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-06-06
  • Android編程實現(xiàn)應(yīng)用獲取包名、版本號、權(quán)限等信息的方法

    Android編程實現(xiàn)應(yīng)用獲取包名、版本號、權(quán)限等信息的方法

    這篇文章主要介紹了Android編程實現(xiàn)應(yīng)用獲取包名、版本號、權(quán)限等信息的方法,涉及Android針對應(yīng)用相關(guān)信息的獲取操作實現(xiàn)技巧,需要的朋友可以參考下
    2018-02-02
  • Android設(shè)置鈴聲實現(xiàn)代碼

    Android設(shè)置鈴聲實現(xiàn)代碼

    這篇文章主要介紹了Android設(shè)置鈴聲實現(xiàn)代碼,以實例形式分析了Android中鈴聲設(shè)置的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
    2015-10-10
  • Flutter Element概念簡明分析

    Flutter Element概念簡明分析

    Flutter 中 Element 作用的是作為中樞來管理和調(diào)度Widget和RenderObject,這里我們主要說一下RenderObjectWidget 來主要說一下Element 的生命周期,這里我刪除了一些assert 的方法,方便查看
    2023-04-04
  • SDL2和OpenGL使用踩坑筆記經(jīng)驗分享

    SDL2和OpenGL使用踩坑筆記經(jīng)驗分享

    今天小編就為大家分享一篇關(guān)于SDL2和OpenGL使用踩坑筆記經(jīng)驗分享,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Android如何實現(xiàn)掃描和生成二維碼

    Android如何實現(xiàn)掃描和生成二維碼

    這篇文章主要為大家詳細介紹了Android如何實現(xiàn)掃描和生成二維碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 利用Android模仿微信攝像圓環(huán)進度效果實例

    利用Android模仿微信攝像圓環(huán)進度效果實例

    圓環(huán)進度條,大家應(yīng)該都見過,而這篇文章主要給大家介紹了關(guān)于利用Android模仿微信攝像圓環(huán)進度效果的相關(guān)資料,實現(xiàn)后的效果非常不錯,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。
    2017-11-11

最新評論

柯坪县| 会昌县| 乐平市| 阳谷县| 海城市| 古丈县| 林西县| 湟源县| 湖北省| 资源县| 昔阳县| 苍南县| 江北区| 乌兰县| 达尔| 哈尔滨市| 青川县| 南华县| 镇远县| 沂南县| 株洲市| 东安县| 商水县| 河津市| 灵石县| 梧州市| 岱山县| 镇沅| 河津市| 出国| 海丰县| 承德市| 合作市| 太和县| 水城县| 铁岭县| 彭州市| 闻喜县| 大悟县| 涞水县| 利津县|