1 package com.example.soundpool; 2 3 import android.media.AudioManager; 4 import android.media.SoundPool; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 10 public class MainActivity extends Activity { 11 private SoundPool soundPool; 12 private int soundId; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 //load是异步的方法,所以要放在这里,若放到fire函数里是不行的 18 soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC,0); 19 soundId = soundPool.load(this, R.raw.qq, 1);//加载声音资源 20 } 21 22 public void fire(View view){ 23 //第二个参数代表左声;第三个代表右声;第四个代表优先级;第五个代表是否循环播放;第六个代表播放速度,范围为0.5-2.0 24 soundPool.play(soundId, 1.0f, 1.0f, 0, 0, 1.0f); 25 } 26 27 }