Android SoundPool简单语音的播放

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值