android SoundPool 的使用

來源:互聯網
上載者:User

Android裡的SoundPool類是 android.media 包裡提供的一個用來播放音效檔的類,可以支援同時播放多個音效檔,可以控制每個檔案的迴圈次數。基本上要遵守下面的步驟: 

1  實現   SoundPool.OnLoadCompleteListener 介面的 onLoadComplete函數

2  New一個SoundPool 的執行個體 sndPool,建構函式的第一個參數指定最多同時播放的檔案個數

3  設定  sndPool的 onLoadCompleteListener 回呼函數

4  sndPool .load 一個音頻檔案,也稱為一個流

5  在  onLoadComplete函數裡判斷成功與否,一般在這裡發一個訊息讓activity來調用 sndPool.play 這個流

6 重複4和5,知道你把所有想播放的流都play起來了

可能遇到的問題點:

1  音頻檔案不能太大,否則會造成AudioCache的Heap size overflow

2  一定要收到 onLoadComplete回調之後再 play

3 要在程式的onDestroy裡調用 sndPool.release(),否則的話會第二次開啟這個程式,不出聲音。

4 SoundPool.OnLoadCompleteListener.onLoadComplete(SoundPool
soundPool, int sampleId, int status) 的第二個參數sampleId 和 SoundPool.load 返回的參數是同一 個東西。

5 下面所附的代碼裡還碰到一個問題,開始的時候沒有在pause等id後面加100,老是出錯,應該是和android內部的定義有衝突。

  

package com.android.testSoundPool;

import android.app.Activity;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.util.Log;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Handler;

public class TestSoundPoolActivity extends Activity implements View.OnClickListener,
                SoundPool.OnLoadCompleteListener  {
 static String TAG="TestSoundPoolActivity" ;
 TextView tv ;
 SoundPool sndPool ;
 int sndid ;
 int[] StreamID =new int[10] ;
 int StreamNum = 0;
 int StreamNumPause = 0  ;
 int[] rid = new int[]{R.raw.iremembershort1,R.raw.in_call_alarm,R.raw.down,R.raw.down,R.raw.down,R.raw.down,
   R.raw.down,R.raw.down,R.raw.down,R.raw.down} ; 
 
 private static final int SOUND_LOAD_OK = 1;
 private final Handler mHandler = new MyHandler() ;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.textvbb100) ;
        tv.setText("1234567");
        Button b1 = (Button)findViewById(R.id.play100) ;
        b1.setOnClickListener(this);       
       
        b1 = (Button)findViewById(R.id.more100) ;
        b1.setOnClickListener(this);
       
        b1 = (Button)findViewById(R.id.pause100) ;
        b1.setOnClickListener(this);
        sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC,0 ) ;
        sndPool.setOnLoadCompleteListener(this);
       
    }
    public void onDestroy()
    {  
     sndPool.release() ;
     super.onDestroy();
    }
    private class MyHandler extends Handler {
     public void handleMessage(Message msg){
      switch( msg.what){
      case SOUND_LOAD_OK:       
       StreamID[StreamNum] = sndPool.play( msg.arg1, (float)0.8,(float)0.8, 16, 10, (float)1.0) ;
       StreamNum ++ ;
       break;
      }
     }
    }
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status)  {
     Message msg = mHandler.obtainMessage(SOUND_LOAD_OK); ;
     msg.arg1 = sampleId ;     
     mHandler.sendMessage(msg);
    }
    public void onClick(View v) {
     int id = v.getId() ;
     switch ( id ) {  
      case R.id.play100:  
       tv.setText("play");
       Log.v(TAG , "play main");
       if( sndPool != null )        
        sndid = sndPool.load( this , rid[StreamNum] , 1 ) ;        
       break ;
      case R.id.more100:
       tv.setText("more");
       sndPool.load( this , rid[StreamNum] , 1 ) ;
       Log.v(TAG , "more");
       break ;
      case R.id.pause100:
       tv.setText("pause");
       if( StreamNumPause< StreamNum ) {
        sndPool.pause(StreamID[StreamNumPause]) ;
        StreamNumPause ++ ;
       }
       Log.v(TAG,"pause");
       break;
     }     
    }
}

可以在http://download.csdn.net/source/3497819下載整個代碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.