• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Android TextToSpeech TTS文文本转语音(语音合成)

武飞扬头像
Ang_qq_252390816
帮助1

简介

TTS即从文本到语音(TextToSpeech)

一,使用第三方提供的SDK或者API,例如:科大讯飞,百度,阿里等;

二,使用Android系统自带的API :TextToSpeech

TextToSpeech中的API文档(官方):TextToSpeech  |  Android Developers,记录各个方法,变量,常量详细介绍;

三,TextToSpeech的使用

如下代码是做了简单封装;

  1.  
    public class TTSUtils extends UtteranceProgressListener {
  2.  
     
  3.  
    private Context mContext;
  4.  
    private static TTSUtils singleton;
  5.  
    private TextToSpeech textToSpeech; // 系统语音播报类
  6.  
    private boolean isSuccess = true;
  7.  
     
  8.  
    public static TTSUtils getInstance(Context context) {
  9.  
    if (singleton == null) {
  10.  
    synchronized (TTSUtils.class) {
  11.  
    if (singleton == null) {
  12.  
    singleton = new TTSUtils(context);
  13.  
    }
  14.  
    }
  15.  
    }
  16.  
    return singleton;
  17.  
    }
  18.  
     
  19.  
    private TTSUtils(Context context) {
  20.  
    this.mContext = context.getApplicationContext();
  21.  
    textToSpeech = new TextToSpeech(mContext, i -> {
  22.  
    //系统语音初始化成功
  23.  
    if (i == TextToSpeech.SUCCESS) {
  24.  
    int result = textToSpeech.setLanguage(Locale.CHINA);
  25.  
    textToSpeech.setPitch(2.0f);// 设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
  26.  
    textToSpeech.setSpeechRate(1.0f);
  27.  
    textToSpeech.setOnUtteranceProgressListener(TTSUtils.this);
  28.  
    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  29.  
    //系统不支持中文播报
  30.  
    isSuccess = false;
  31.  
    }
  32.  
    }
  33.  
     
  34.  
    });//百度的播放引擎 "com.百度.duersdk.opensdk"
  35.  
     
  36.  
    }
  37.  
     
  38.  
    public void playText(String playText) {
  39.  
     
  40.  
    if (!isSuccess) {
  41.  
    Toast.makeText(mContext, "系统不支持中文播报", Toast.LENGTH_SHORT).show();
  42.  
    return;
  43.  
    }
  44.  
     
  45.  
    if (textToSpeech != null) {
  46.  
    textToSpeech.speak(playText, TextToSpeech.QUEUE_ADD, null, null);
  47.  
    }
  48.  
    }
  49.  
     
  50.  
    public void stopSpeak() {
  51.  
    if (textToSpeech != null) {
  52.  
    textToSpeech.stop();
  53.  
    textToSpeech.shutdown();
  54.  
     
  55.  
    }
  56.  
    }
  57.  
     
  58.  
    @Override
  59.  
    public void onStart(String utteranceId) {
  60.  
     
  61.  
    }
  62.  
     
  63.  
    @Override
  64.  
    public void onDone(String utteranceId) {
  65.  
     
  66.  
    }
  67.  
     
  68.  
    @Override
  69.  
    public void onError(String utteranceId) {
  70.  
     
  71.  
    }
  72.  
    }
学新通

使用

在Application中进行初始化,或者在Activity中先初始化,因为TextToSpeech初始化需要时间,所以在使用之前早点初始化,不然有坑;

TTSUtils.getInstance(this);

调用

TTSUtils.getInstance(getContext()).playText("床前明月光,疑似地上霜");

问题

没有声音或者提示不支持中文

1,需要检查系统是否设置播放引擎,荣耀V30为例 设置—>辅助功能 —>无障碍 —> 文本转语音中是否安装了播放引擎;系统默认是应该是Pico TTS,不过这个不支持中文;

关于默认引擎也可以通过TextToSpeech中的

getDefaultEngine()

方法获取;

需要安装第三方播放引擎,比如:com.谷歌.android.tts 谷歌文字转语音引擎,不支持5.0以下系统;com.iflytek.speechcloud 科大讯飞语音引擎3.0,支持4.0以上系统;com.百度.duersdk.opensdk 度秘语音引擎3.0 不支持5.0以下系统;

百度网盘下载地址 密码:78td

2,根据自己需要安装引擎

3,在代码中设置引擎

在创建TextToSpeech的时候有个构造方法可以指定引擎;

public TextToSpeech(Context context, OnInitListener listener, String engine) { //engine就是要制定的引擎,这个传参其实就是引擎的包名,百度的播放引擎 "com.百度.duersdk.opensdk" this(context, listener, engine, null, true); }

engine就是要制定的引擎,这个传参其实就是引擎的包名,百度的播放引擎 "com.百度.duersdk.opensdk"

4,如果不在代码中设置,可以在Android设备的设置中找到“文本转语音”(一般在“无障碍”菜单下)修改自己的引擎即可,替换掉系统默认Pico TTS;

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfjkf
系列文章
更多 icon
同类精品
更多 icon
继续加载