欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Android

當(dāng)前位置:主頁 > 軟件編程 > Android >

Android使用SoundPool 音效實例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊: 次

使用場景

SoundPool一般用來 密集,急促而又短暫的音效,比如特技音效:Duang~,游戲用得較多,你也可以為你的 APP添加上這個音效,比如酷狗音樂進去的時候 "哈嘍,酷狗" 是不是提起了對于SoundPool的興趣了呢

ok,廢話不多說 詳細的參數(shù)解釋請看注釋

public class SoundPlayer extends AppCompatActivity {

  private SoundPool mSoundPool;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sound_player);

    initState();
  }

  private void initState() {
    //sdk版本21是SoundPool 的一個分水嶺
    if (Build.VERSION.SDK_INT >= 21) {
      SoundPool.Builder builder = new SoundPool.Builder();
      //傳入最多      音頻數(shù)量,
      builder.setMaxStreams(1);
      //AudioAttributes是一個封裝音頻各種屬性的方法
      AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
      //設(shè)置音頻流的合適的屬性
      attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
      //加載一個AudioAttributes
      builder.setAudioAttributes(attrBuilder.build());
      mSoundPool = builder.build();
    } else {
      /**
       * 第一個參數(shù):int maxStreams:SoundPool對象的最大并發(fā)流數(shù)
       * 第二個參數(shù):int streamType:AudioManager中描述的音頻流類型
       *第三個參數(shù):int srcQuality:采樣率轉(zhuǎn)換器的質(zhì)量。 目前沒有效果。 使用0作為默認值。
       */
      mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    }

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

    //1.通過一個AssetFileDescriptor對象
    //int load(AssetFileDescriptor afd, int priority)
    //2.通過一個資源ID
    //int load(Context context, int resId, int priority)
    //3.通過指定的路徑加載
    //int load(String path, int priority)
    //4.通過FileDescriptor加載
    //int load(FileDescriptor fd, long offset, long length, int priority)
    //聲音ID 加載音頻資源,這里用的是第二種,第三個參數(shù)為priority,聲音的優(yōu)先級*API中指出,priority參數(shù)目前沒有效果,建議設(shè)置為1。
    final int voiceId = mSoundPool.load(this, R.raw.duang, 1);
    //異步需要等待加載完成,音頻才能      成功
    mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        if (status == 0) {
          //第一個參數(shù)soundID
          //第二個參數(shù)leftVolume為左側(cè)音量值(范圍= 0.0到1.0)
          //第三個參數(shù)rightVolume為右的音量值(范圍= 0.0到1.0)
          //第四個參數(shù)priority 為流的優(yōu)先級,值越大優(yōu)先級高,影響當(dāng)同時      數(shù)量超出了最大支持數(shù)時SoundPool對該流的處理
          //第五個參數(shù)loop 為音頻重復(fù)      次數(shù),0為值      一次,-1為無限循環(huán),其他值為      loop+1次
          //第六個參數(shù) rate為      的速率,范圍0.5-2.0(0.5為一半速率,1.0為正常速率,2.0為兩倍速率)
          soundPool.play(voiceId, 1, 1, 1, 0, 1);
        }
      }
    });
  }
  }

非常簡單的使用。

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

上一篇:flutter 中監(jiān)聽滑動事件

欄    目:Android

下一篇:Android自定義底部彈出框ButtomDialog

本文標題:Android使用SoundPool 音效實例

本文地址:http://mengdiqiu.com.cn/a1/Android/9030.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有