- SoundPool是用来播放短促声音的对象,他最大支持存放20个短促的声音
- 具体使用的代码请参见下面的demo示例
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
private SoundPool soundPool;
private Map<String,Integer> poolMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
poolMap = new HashMap<String, Integer>();
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
poolMap.put("test", soundPool.load(this, R.raw.ce, 1));
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
if (sampleId == poolMap.size()) {
MainActivity.this.soundPool.play(poolMap.get("test"), 1.0f, 1.0f, 0, 3,
1.0f);
}
}
});
}
private void init() {
button= (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
if (soundPool != null) {
soundPool.play(poolMap.get("test"), 1.0f, 1.0f, 0, -1, 1.0f);
}
break;
}
}
}