MediaPlayer的缺点:
资源占用量高,延时时间较长
不支持多个音效同时播放
SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音量,播放比率等参数,避免使用SoundPool来播放歌曲或者做游戏背景音乐,只有那些短促的密集的声音才考虑使用SoundPool播放
构造器:
public SoundPool (int maxStreams, int streamType, int srcQuality)
Parameters
maxStreams the maximum number of simultaneous streams for this SoundPool object
streamType the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.
srcQuality the sample-rate converter quality. Currently has no effect. Use 0 for the default.
Returns
a SoundPool object, or null if creation failed
加载声音:
public int load (AssetFileDescriptor afd, int priority)
Parameters
afd an asset file descriptor
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (Context context, int resId, int priority)
Parameters
context the application context
resId the resource ID
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (String path, int priority)
Parameters
path the path to the audio file
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
public int load (FileDescriptor fd, long offset, long length, int priority)
Parameters
fd a FileDescriptor object
offset offset to the start of the sound
length length of the sound
priority the priority of the sound. Currently has no effect. Use a value of 1 for future compatibility.
播放声音:
public final int play (int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)
使用SoundPool播放声音的步骤:Parameters
soundID a soundID returned by the load() function
leftVolume left volume value (range = 0.0 to 1.0)
rightVolume right volume value (range = 0.0 to 1.0)
priority stream priority (0 = lowest priority)
loop loop mode (0 = no loop, -1 = loop forever)
rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
调用SoundPool的构造器创建SoundPool的对象
调用SoundPool对象的load()方法从指定资源,文件,中加载声音,最好使用HashMap<Integer,Integer>来管理所加载的声音
调用SoundPool的play方法播放声音
例子程序:
import java.util.HashMap; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SoundPoolTest extends Activity implements OnClickListener { Button bomb, shot, arrow; // 定义一个SoundPool SoundPool soundPool; HashMap<Integer, Integer> soundMap = new HashMap<Integer, Integer>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bomb = (Button) findViewById(R.id.bomb); shot = (Button) findViewById(R.id.shot); arrow = (Button) findViewById(R.id.arrow); // 设置最多可容纳10个音频流,音频的品质为5 soundPool = new SoundPool(10 , AudioManager.STREAM_SYSTEM, 5); //① // load方法加载指定音频文件,并返回所加载的音频ID。 // 此处使用HashMap来管理这些音频流 soundMap.put(1, soundPool.load(this, R.raw.bomb, 1)); //② soundMap.put(2, soundPool.load(this, R.raw.shot, 1)); soundMap.put(3, soundPool.load(this, R.raw.arrow, 1)); bomb.setOnClickListener(this); shot.setOnClickListener(this); arrow.setOnClickListener(this); } // 重写OnClickListener监听器接口的方法 @Override public void onClick(View source) { // 判断哪个按钮被单击 switch (source.getId()) { case R.id.bomb: soundPool.play(soundMap.get(1), 1, 1, 0, 0, 1); //③ break; case R.id.shot: soundPool.play(soundMap.get(2), 1, 1, 0, 0, 1); break; case R.id.arrow: soundPool.play(soundMap.get(3), 1, 1, 0, 0, 1); break; } } }