android开发:播放音频功能的工具类

播放音频功能的工具类


/**
 * 播放声音工具类
 * creator: ZZF
 * careate date: 2018/5/25  10:36.
 */

public class SoundUtils {

    private SoundPool pool;
    private  List<Object> listPool = new ArrayList<Object>();
    private boolean isOpen;//是否播放声音
    public void initPool(Context context) {
        setOpen(true);

        pool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
//这里添加自己的需要的音频文件
        listPool.add(pool.load(context, R.raw.wifi_connected, 0));//wifi已连接 1
        listPool.add(pool.load(context, R.raw.wiifi_disconected, 0));//wifi已断开 2
        listPool.add(pool.load(context, R.raw.beep, 0));//蜂鸣器 2
    }

    private static SoundUtils instance = null;

    private SoundUtils(){
    }

    public static SoundUtils getInstance() {
        synchronized (SoundUtils.class) {
            if (instance == null) {
                instance = new SoundUtils();
            }
        }

        return instance;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    /**
     * list 坐标位置
     * @param flag
     */
    public  void playVoice(int flag) {
        boolean b = isOpen();
        if(b){
            // 消费成功 提示声音
            if (null != pool) {
                int index = (Integer) listPool.get(flag);
                pool.play(index, 1, 1, 0, 0, 1);
            }
        }else if(flag == 0 && !b){
            flag = (listPool.size()-1);
            if (null != pool) {
                int index = (Integer) listPool.get(flag);
                pool.play(index, 1, 1, 0, 0, 1);
            }
        }else{
            System.out.println("===========语音提示关闭");
        }
    }
}

使用步骤:

1初始化

在Acitvity或fragment中的onCreate方法

 SoundUtils.getInstance().initPool(this);
2 调用
 SoundUtils.getInstance().playVoice(1);
文字转语音的工具

点击:文字转语音的工具

生成的音频文件,放到android项目中的res/raw目录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值