Android SoundPool語音播報(bào)工具類

項(xiàng)目中需要用到語音提示,且都是比較簡短的語音提示,剛開始的時(shí)候用的是MediaPlayer進(jìn)行語音播放,但是查閱資料發(fā)現(xiàn)MediaPlayer主要是用來進(jìn)行長音頻播放,且資源消耗較高,后發(fā)現(xiàn)SoundPool進(jìn)行語音播放時(shí)資源消耗更少,且響應(yīng)速度更快,就寫了一個(gè)簡單的SoundPool工具類,主要是用來進(jìn)行短音頻播放,還可以判斷當(dāng)前是否有音頻正在播放。
工具類代碼如下:

/**
 * 簡短音頻播放工具類
 */
public class SoundPoolUtil {
    private volatile static SoundPoolUtil client;
    private SoundPool mSoundPool;
    private AudioManager mAudioManager;
    /*允許同時(shí)播放的音頻數(shù)(為1時(shí)會(huì)立即結(jié)束上一個(gè)音頻播放當(dāng)前的音頻)*/
    private static final int MAX_STREAMS = 1;
    // Stream type.
    private static final int streamType = AudioManager.STREAM_MUSIC;
    private int mSoundId;
    private int mResId;
    private Context mainContext;

    public static SoundPoolUtil getInstance(Context context) {
        if (client == null)
            synchronized (SoundPoolUtil.class) {
                if (client == null) {
                    client = new SoundPoolUtil(context);
                }
            }
        return client;
    }

    private SoundPoolUtil(Context context) {
        this.mainContext = context;
        mAudioManager = (AudioManager) this.mainContext.getSystemService(Context.AUDIO_SERVICE);
        ((Activity) this.mainContext).setVolumeControlStream(streamType);
        this.mSoundPool = new SoundPool(MAX_STREAMS, streamType, 0);
    }


    /**
     * 播放音頻
     * @param resId 本地音頻資源
     */
    public void playSoundWithRedId(int resId) {
        this.mResId = resId;
        this.mSoundId = this.mSoundPool.load(this.mainContext, this.mResId, 1);
        this.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                playSound();
            }
        });

    }
    
    /**
     * 播放音頻,但是當(dāng)前有音頻這正播放中時(shí)不響應(yīng)該次音頻播放
     * @param resId 本地音頻資源
     */
    public synchronized void playSoundUnfinished(int resId) {
        if ( isFmActive()) return;
        this.mResId = resId;
        this.mSoundId = this.mSoundPool.load(this.mainContext, this.mResId, 1);
        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                playSound();
            }
        });
    }

    /**
     * 播放音頻文件
     */
    private void playSound() {
        mSoundPool.play(this.mSoundId, 1.0f, 1.0f, 0, 0, 1f);
    }

    /**
     * 判斷當(dāng)前設(shè)備是否正在播放音頻
     */
    private boolean isFmActive() {
        if (mAudioManager == null) {
            return false;
        }
        return mAudioManager.isMusicActive();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、簡歷準(zhǔn)備 1、個(gè)人技能 (1)自定義控件、UI設(shè)計(jì)、常用動(dòng)畫特效 自定義控件 ①為什么要自定義控件? Andr...
    lucas777閱讀 5,378評(píng)論 2 54
  • android音頻處理 音頻基礎(chǔ)概念 基礎(chǔ)知識(shí)可以參考以下幾篇文章: 音頻,PCM,采樣率及android的Aud...
    好奇的小刺猬閱讀 2,668評(píng)論 0 3
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,317評(píng)論 0 17
  • 那天周末,兒子他們和朋友一家在外相聚。席間,帶著老媽的朋友問起我們?yōu)楹尾灰黄饋??兒子答道,玩去啦。他們感到驚訝,倆...
    用心生活用字記錄閱讀 983評(píng)論 6 6
  • 日精進(jìn):敬畏—進(jìn)入—體驗(yàn)—交給—持續(xù) 1,缺啥補(bǔ)啥,怕啥練啥; 2,切為我所用,所用為團(tuán)隊(duì)家; 3,我想變,我要...
    xiebo閱讀 187評(píng)論 0 0

友情鏈接更多精彩內(nèi)容