当前位置:   article > 正文

【Android】Android 集成讯飞SDK实现语音播报_com.iflytek.speechcloud

com.iflytek.speechcloud

【Android】Android 集成讯飞SDK实现语音播报

1、集成讯飞语音依赖

登录讯飞语音平台,创建一个应用,并领取免费的在线语音合成,总的可以领取到55万条,有效期一年
在这里插入图片描述

然后下载SDK放到lib下

implementation files('libs/Msc.jar')
  • 1

2、封装讯飞语音工具类

import android.os.Bundle;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.zhhy.qneatrice.app.cashier.MainActivity;

/**
 * 讯飞语音
 *
 * @author Tellsea
 * @date 2021-10-18
 */
public class MscUtils {

    public static void initIflytek() {
        SpeechUtility.createUtility(MainActivity.CONTEXT, SpeechConstant.APPID + "=应用APPID");
    }

    public static void speekText(String text) {
        //1. 创建 SpeechSynthesizer 对象 , 第二个参数: 本地合成时传 InitListener
        SpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(MainActivity.CONTEXT, null);
//2.合成参数设置,详见《 MSC Reference Manual》 SpeechSynthesizer 类
//设置发音人(更多在线发音人,用户可参见 附录 13.2
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan"); // 设置发音人
        mTts.setParameter(SpeechConstant.SPEED, "50");// 设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "100");// 设置音量,范围 0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
//3.开始合成
        mTts.startSpeaking(text, new MySynthesizerListener());

    }

    static class MySynthesizerListener implements SynthesizerListener {

        @Override
        public void onSpeakBegin() {
        }

        @Override
        public void onSpeakPaused() {
        }

        @Override
        public void onSpeakResumed() {
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
            // 合成进度
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            // 播放进度
        }

        @Override
        public void onCompleted(SpeechError error) {
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

3、MainActivity初始化讯飞语音

// 初始化讯飞
MscUtils.initIflytek();
  • 1
  • 2

4、调用语音播报

MscUtils.speekText("识别失败,正在重试");
  • 1

技术分享区

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/830510?site
推荐阅读
相关标签
  

闽ICP备14008679号