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

版权声明:本文为博主原创文章,未经博主允许不得转载,如需转载请标明出处 https://blog.csdn.net/qq_32368129/article/details/53838281

一开始为了赶项目进度,没进行封装工具类,在好几个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 写一遍就行了 如果不需要改参数的话





作者水平有限     不喜勿喷

展开阅读全文

没有更多推荐了,返回首页