Android 离线语音(讯飞语音)封装工具类

一开始为了赶项目进度,没进行封装工具类,在好几个Actiivty里写了好几遍重复代码,

感觉太冗余了,今天把代码封装一下,使用起来就很方便了


如果对离线语音配置与使用方法不是很了解

请先阅读我上一篇文章Android 离线语音使用方法与配置


工具类

Myapplication:
    public void initVoice(){
      //初始化语音播报
      StringBuffer param = new StringBuffer();
      param.append("appid=" + "APPID");
      param.append(",");
      param.append(SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC);
      SpeechUtility.createUtility(getApplicationContext(), param.toString());
   }


util:

import android.content.Context;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.util.ResourceUtil;

/**
 * Created by ly on 2016/12/22.
 */

public class OfflineVoiceUtils {
    // 语音合成对象
    public static SpeechSynthesizer mTts;
    // 默认本地发音人
    public static String voicerLocal = "xiaoyan";
    Context context;
    private static OfflineVoiceUtils instance = null;


    // 获取发音人资源路径
    public String getResourcePath() {
        StringBuffer tempBuffer = new StringBuffer();
        // 合成通用资源
        tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, "tts/common.jet"));
        tempBuffer.append(";");
        // 发音人资源
        tempBuffer.append(ResourceUtil.generateResourcePath(context, ResourceUtil.RESOURCE_TYPE.assets, "tts/" + OfflineVoiceUtils.voicerLocal + ".jet"));
        return tempBuffer.toString();
    }

    public void setParam(String voice_text_rate) {
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        // 设置使用本地引擎
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
        // 设置发音人资源路径
        mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
        // 设置发音人
        mTts.setParameter(SpeechConstant.VOICE_NAME, voicerLocal);
        // 设置语速
        mTts.setParameter(SpeechConstant.SPEED, voice_text_rate);
        // 设置音调
        mTts.setParameter(SpeechConstant.PITCH, "50");
        // 设置音量
        mTts.setParameter(SpeechConstant.VOLUME, "50");
        // 设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
    }

    public static OfflineVoiceUtils getInstance(Context context) {
        if (instance == null)
            instance = new OfflineVoiceUtils();
        instance.setContext(context);
        mTts = SpeechSynthesizer.createSynthesizer(context, new InitListener() {
            @Override
            public void onInit(int code) {
                if (code != ErrorCode.SUCCESS) {
//                    Toast.makeText(mContext, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT).show();
                }
            }
        });
        return instance;
    }

    public void setContext(Context context) {
        this.context = context;
    }
}


Activity:(使用的地方)


onCreate:
 OfflineVoiceUtils.getInstance(this).setParam();
 OfflineVoiceUtils.mTts.startSpeaking("我爱你中国",null);

setParam 写一遍就行了 如果不需要改参数的话





作者水平有限     不喜勿喷

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值