1、在res目录下建立一个raw文件夹,然后把.wav文件放在这个文件夹下面
2、建一个类用于加载和播放语音如下:
public class SoundPoolUtil {
public static SoundPoolUtil instance;
private SoundPool soundPool;
private HashMap<Integer, Integer> soundMap;
private SoundPoolUtil(){}
public static SoundPoolUtil getInstance(){
if (instance == null){
instance = new SoundPoolUtil();
}
return instance;
}
public void loadWav(Context context){
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundMap = new HashMap<>();
soundMap.put(1, soundPool.load(context, R.raw.welcome, 1));
soundMap.put(2, soundPool.load(context, R.raw.apprise, 1));
soundMap.put(3, soundPool.load(context, R.raw.shuru, 1));
soundMap.put(4, soundPool.load(context, R.raw.retshuru, 1));
soundMap.put(5, soundPool.load(context, R.raw.thanks, 1));
}
//i表示要播放那个语音
public void playWav(int i){
soundPool.play(soundMap.get(i), 1, 1, 0, 0, 1);
}
}
3、进行简单的调用
SoundPoolUtil soundPoolUtil = SoundPoolUtil.getInstance();
soundPoolUtil.loadWav(this);
soundPoolUtil.playWav(1);