欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用SoundPool播放音效實(shí)例

 更新時(shí)間:2019年11月29日 10:23:11   作者:安東尼肉店  
這篇文章主要為大家詳細(xì)介紹了Android使用SoundPool播放音效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用場(chǎng)景

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

ok,廢話不多說(shuō) 詳細(xì)的參數(shù)解釋請(qǐng)看注釋

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

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

    //1.通過(guò)一個(gè)AssetFileDescriptor對(duì)象
    //int load(AssetFileDescriptor afd, int priority)
    //2.通過(guò)一個(gè)資源ID
    //int load(Context context, int resId, int priority)
    //3.通過(guò)指定的路徑加載
    //int load(String path, int priority)
    //4.通過(guò)FileDescriptor加載
    //int load(FileDescriptor fd, long offset, long length, int priority)
    //聲音ID 加載音頻資源,這里用的是第二種,第三個(gè)參數(shù)為priority,聲音的優(yōu)先級(jí)*API中指出,priority參數(shù)目前沒(méi)有效果,建議設(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) {
          //第一個(gè)參數(shù)soundID
          //第二個(gè)參數(shù)leftVolume為左側(cè)音量值(范圍= 0.0到1.0)
          //第三個(gè)參數(shù)rightVolume為右的音量值(范圍= 0.0到1.0)
          //第四個(gè)參數(shù)priority 為流的優(yōu)先級(jí),值越大優(yōu)先級(jí)高,影響當(dāng)同時(shí)播放數(shù)量超出了最大支持?jǐn)?shù)時(shí)SoundPool對(duì)該流的處理
          //第五個(gè)參數(shù)loop 為音頻重復(fù)播放次數(shù),0為值播放一次,-1為無(wú)限循環(huán),其他值為播放loop+1次
          //第六個(gè)參數(shù) rate為播放的速率,范圍0.5-2.0(0.5為一半速率,1.0為正常速率,2.0為兩倍速率)
          soundPool.play(voiceId, 1, 1, 1, 0, 1);
        }
      }
    });
  }
  }

非常簡(jiǎn)單的使用。

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

相關(guān)文章

最新評(píng)論