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

Android中SoundPool的使用步驟實例

 更新時間:2019年03月27日 11:30:31   作者:徐劉根  
今天小編就為大家分享一篇關(guān)于Android中SoundPool的使用步驟實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

大家知道MediaPlayer占用的資源比較多,且不可以同時支持播放多個音頻,所以我們有一種叫做SoundPool,比如我們常見的按鍵音或者是手機(jī)提示音,還比如我們在游戲的開發(fā)中會有大量的音效效果等,下邊介紹一下她的用法:

步驟如下:

1.創(chuàng)建SoundPool對象

源碼如下

 /**
   *SoundPool源碼中的構(gòu)造方法方法體
   * @param maxStreams 最多可以容納多少個音頻
   * @param streamType 指定的聲音類型,通過AudioManager類提供的常量進(jìn)行指定
   * @param srcQuality 指定音頻的質(zhì)量,默認(rèn)為0
   * @return a SoundPool object, or null if creation failed
   */
  public SoundPool(int maxStreams, int streamType, int srcQuality)

2.加載所需要播放的音頻:

/**
   * @param context the application context
   * @param resId the resource ID
   * @param priority the priority of the sound. Currently has no effect. Use
   *         a value of 1 for future compatibility.
   * @return a sound ID. This value can be used to play or unload the sound.
   */
 public int load(Context context, int resId, int priority);

3.播放音頻

 /**
   * Play a sound from a sound ID.
   * @param soundID 通過load方法返回的音頻
   * @param leftVolume 左聲道的音量
   * @param rightVolume 右聲道的音量 
   * @param priority 優(yōu)先級,值越大,優(yōu)先級越高
   * @param loop 循環(huán)的次數(shù):0為不循環(huán),-1為循環(huán)
   * @param rate 指定速率,正常位1,為地位0.5,最高位2
   * @return non-zero streamID if successful, zero if failed
   */
  public final int play(int soundID, float leftVolume, float rightVolume,
      int priority, int loop, float rate);

4.案例如下:

(1)布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="風(fēng)鈴聲" />
  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="布谷鳥叫聲" />
  <Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="門鈴聲" />
  <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="電話聲" />
</LinearLayout>

(2)MainActivity.java文件

package com.mingrisoft;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
  private SoundPool soundpool;  //聲明一個SoundPool對象
  //使用HashMap管理各種音頻
  private HashMap<Integer, Integer> soundmap = new HashMap<Integer, Integer>();  //創(chuàng)建一個HashMap對象
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button chimes = (Button) findViewById(R.id.button1);  //獲取“風(fēng)鈴聲”按鈕
    Button enter = (Button) findViewById(R.id.button2);   //獲取“布谷鳥叫聲”按鈕
    Button notify = (Button) findViewById(R.id.button3);  //獲取“門鈴聲”按鈕
    Button ringout = (Button) findViewById(R.id.button4);  //獲取“電話聲”按鈕
    soundpool = new SoundPool(5,
        AudioManager.STREAM_SYSTEM, 0); //創(chuàng)建一個SoundPool對象,該對象可以容納5個音頻流
    //將要播放的音頻流保存到HashMap對象中
    soundmap.put(1, soundpool.load(this, R.raw.chimes, 1));
    soundmap.put(2, soundpool.load(this, R.raw.enter, 1));
    soundmap.put(3, soundpool.load(this, R.raw.notify, 1));
    soundmap.put(4, soundpool.load(this, R.raw.ringout, 1));
    soundmap.put(5, soundpool.load(this, R.raw.ding, 1));
    //為各按鈕添加單擊事件監(jiān)聽器
    chimes.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(1), 1, 1, 0, 0, 1); //播放指定的音頻
      }
    });
    enter.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(2), 1, 1, 0, 0, 1);//播放指定的音頻
      }
    });
    notify.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(3), 1, 1, 0, 0, 1);//播放指定的音頻
      }
    });
    ringout.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        soundpool.play(soundmap.get(4), 1, 1, 0, 0, 1);//播放指定的音頻
        soundpool.play(soundpool.load(MainActivity.this, R.raw.notify, 1), 1, 1, 0, 0, 1);
      }
    });
  }
  //重寫鍵被按下的事件
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    soundpool.play(soundmap.get(5), 1, 1, 0, 0, 1);   //播放按鍵音
    return true;
  }
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Android手機(jī)開發(fā) 使用線性布局和相對布局實現(xiàn)Button垂直水平居中

    Android手機(jī)開發(fā) 使用線性布局和相對布局實現(xiàn)Button垂直水平居中

    本文主要結(jié)合自己的理解分別對使用LinearLayout和RelativeLayout兩種方式實現(xiàn)居中做了總結(jié),希望對大家有所幫助。
    2016-05-05
  • android使用ViewPager組件實現(xiàn)app引導(dǎo)查看頁面

    android使用ViewPager組件實現(xiàn)app引導(dǎo)查看頁面

    這篇文章主要為大家詳細(xì)介紹了android使用ViewPager組件實現(xiàn)app引導(dǎo)查看頁面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android實現(xiàn)語音數(shù)據(jù)實時采集、播放

    Android實現(xiàn)語音數(shù)據(jù)實時采集、播放

    這篇文章主要介紹了android實現(xiàn)語音數(shù)據(jù)實時采集、播放的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android布局生成分享圖片代碼實例

    Android布局生成分享圖片代碼實例

    這篇文章主要介紹了Android布局生成分享圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Android仿IOS回彈效果 支持任何控件

    Android仿IOS回彈效果 支持任何控件

    這篇文章主要為大家詳細(xì)介紹了Android仿IOS回彈效果,支持任何控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • flutter  TextField換行自適應(yīng)的實現(xiàn)

    flutter TextField換行自適應(yīng)的實現(xiàn)

    這篇文章主要介紹了flutter TextField換行自適應(yīng)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Android?Jetpack庫剖析之LiveData組件篇

    Android?Jetpack庫剖析之LiveData組件篇

    LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)
    2022-07-07
  • Android實現(xiàn)固定屏幕顯示的方法

    Android實現(xiàn)固定屏幕顯示的方法

    這篇文章主要介紹了Android實現(xiàn)固定屏幕顯示的方法,實例分析了Android屏幕固定顯示所涉及的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Android實現(xiàn)語音播放與錄音功能

    Android實現(xiàn)語音播放與錄音功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)語音播放與錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android編程實現(xiàn)輸入框動態(tài)自動提示功能

    Android編程實現(xiàn)輸入框動態(tài)自動提示功能

    這篇文章主要介紹了Android編程實現(xiàn)輸入框動態(tài)自動提示功能,結(jié)合實例形式分析了AutoCompleteTextView相關(guān)使用技巧,需要的朋友可以參考下
    2017-03-03

最新評論

乐昌市| 察哈| 榆林市| 宁化县| 林芝县| 工布江达县| 镇平县| 上林县| 高淳县| 太原市| 搜索| 温泉县| 开平市| 宁城县| 安宁市| 遵义市| 桂平市| 朔州市| 漳州市| 建阳市| 镇安县| 卓资县| 博白县| 澄城县| 博野县| 盱眙县| 浠水县| 绥阳县| 岳普湖县| 大埔县| 海林市| 安远县| 嘉黎县| 南木林县| 澳门| 富锦市| 烟台市| 重庆市| 修水县| 固原市| 贵定县|