Android的SoundPool

來源:互聯網
上載者:User

標籤:des   android   使用   io   strong   檔案   資料   問題   sp   

開發Android軟體中我們可能經常需播放多媒體音效檔,一般使用MediaPlayer類但該類佔用資源較多,對於遊戲等應用可能不是很適合,SoundPool類在SDKandroid.media.SoundPool,顧名思義是聲音池的意思。主要播放一些較短的聲音片段,可以從程式的資源或檔案系統載入,相對於MediaPlayer類可以做到使用較少的CPU資源和較短的反應延遲。

            SoundPool和其他聲音播放類相比,其特點是可以自行設定聲音的品質、音量、播放比率等參等。並且它可以同時管理多個音頻流,每個流都有獨自的ID,對某個音頻流的管理都是通過ID進行的。

         SoundPool基本使用方法:
              1. 建立一個SoundPool 
                 建立一個SoundPool對象:new SoundPool(int maxStreams, int streamType, int srcQuality);

                public SoundPool(int maxStream, int streamType, int srcQuality) 
                maxStream —— 同時播放的流的最大數量 
                  streamType —— 流的類型,一般為STREAM_MUSIC(具體在AudioManager類中列出) 
                  srcQuality —— 採樣率轉化品質,當前無效果,使用0作為預設值 
                  eg. 
                    SoundPool soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);

                       建立了一個最多支援3個流同時播放的,類型標記為音樂的SoundPool。
          2、 從資源或者檔案載入音頻流: load(Context context, int resId, int priority);

                    soundpool的載入: 
                       int  load(Context context, int resId, int priority)  //從APK資源載入 
                       int  load(FileDescriptor fd, long offset, long length, int priority)  //從FileDescriptor對象載入 
                       int  load(AssetFileDescriptor afd, int priority)  //從Asset對象載入 
                       int  load(String path, int priority)  //從完整檔案路徑名載入 
                                  最後一個參數為優先順序。

                         一般把多個聲音放到HashMap中去,比如 
                         soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); 
                         soundPoolMap = new HashMap<Integer, Integer>();   
                         soundPoolMap.put(1, soundPool.load(this, R.raw.dingdong, 1));
          3、  播放聲音play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate);

                     play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) ,

                       其中leftVolume和rightVolume表示左右音量,

                            priority表示優先順序,

                            loop表示迴圈次數,

                           rate表示速率,如  //速率最低0.5最高為2,1代表正常速度 
                    sp.play(soundId, 1, 1, 0, 0, 1); 
                    而停止則可以使用 pause(int streamID) 方法,這裡的streamID和soundID均在構造SoundPool類的

                    第一個參數中指明了總數量,而id從0開始。

 

   1. SoundPool最大隻能申請1M的記憶體空間,這就意味著我們只能用一些很短的聲音片段,而不是用它來播放歌 曲或者做遊戲背景音樂。

   2. SoundPool提供了pausestop方法,但這些方法建議最好不要輕易使用,因為有些時候它們可能會使你的程式莫名其妙的終止。Android開發網建議使用這兩個方法的時候儘可能多做測試工作,還有些朋友反映它們不會立即中止播放聲音,而是把緩衝區裡的資料播放完才會停下來,也許會多播放一秒鐘。

   3. SoundPool的效率問題。其實SoundPool的效率在這些播放類中算是很好的了,這可能會影響使用者體驗。也許這不能管SoundPool本身,因為到了效能比較好的Droid中這個延遲就可以讓人接受了。

Android的SoundPool

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.