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;
}
}
}