当前位置:   article > 正文

Android 离线文字转语音功能-TTS(Text To Speech)_texttospeech

texttospeech

前言

     在 Android 中,TTS全称叫做 Text to Speech,从字面就能理解它解决的问题是什么,把文本转为语音服务,意思就是你输入一段文本信息,然后Android 系统可以把这段文字播报出来。这种应用场景目前比较多是在各种语音助手APP上,很多手机系统集成商内部都有内置文本转语音服务,可以读当前页面上的文本信息。同样,在一些阅读类APP上我们也能看到相关服务,打开微信读书,里面就直接可以把当前页面直接用语音方式播放出来,特别适合哪种不方便拿着手机屏幕阅读的场景。

     Android系统从1.6版本开始就支持TTS, 不过遗憾的是系统默认的TTS引擎:Pico TTS,并不支持中文。

     在查找了许相关资料后, 发现很多的资源都已经失效, APK找不到, 对应的页面404. 更多的厂商则转成了开放平台甚至不再提供离线/免费服务.

目前找到的离线TTS

注:下面的链接可能需要科学上网才可以访问

应用名称支持离线备注下载地址
ITRI TTS下载
讯飞语记需打开一次, 但不需要登陆下载
科大讯飞语音引擎3.0推荐下载
Speech Services by Google需要科学上网, 离线需要先下载语音包下载

使用

首先, 安装对应的TTS应用, 再在设置中设置对应的语音输出服务
一般设置的步骤为: 设置>无障碍>文字转语音(TTS)输出
在这里插入图片描述
程序调用只需使用SDK标准的TTS接口即可.
在介绍TTS使用的文章中, 下面这一段的代码, 对于前面的科大讯飞或ITRI TTS并没什么效果, 只是多了一个弹窗, 当然, 也可以理解为, 这两个APP并没有按标准接口开发.

//显示应用选择窗.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

private TextToSpeech mTts;
protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(this, this);
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

在程序中打印TTS所支持的语言及声音getAvailableLanguagesgetVoices:

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Set<Locale> ava = mTts.getAvailableLanguages();
            if (ava != null) {
                for (Locale l : ava) {
                    Logger.v(TAG, "supported " + l.toString());
                }
            } else {
                Logger.d(TAG, "no supported language");
            }
            Set<Voice> voices = mTts.getVoices();
            if(voices != null){
                for(Voice v : voices){
                    Logger.d(TAG, "TTS Voice: " + v.toString());
                }
            }
        }
        String engine = mTts.getDefaultEngine();
        Logger.d(TAG, "TTS Engine = " + engine);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
2022-10-18 15:21:22.577 25266-25266 TextToSpeech       com.tts.test         I  Connected to ComponentInfo{tw.pu.tts/tw.pu.tts.UttsService}
2022-10-18 15:21:22.582 25266-26006 TextToSpeech       com.tts.test         I  Set up connection to ComponentInfo{tw.pu.tts/tw.pu.tts.UttsService}
2022-10-18 15:21:22.594 25266-25266 TTS                com.tts.test         D   onInit 0
2022-10-18 15:21:22.650 25266-25266 TTS                com.tts.test         V   supported zh
2022-10-18 15:21:22.650 25266-25266 TTS                com.tts.test         V   supported en
2022-10-18 15:21:22.650 25266-25266 TTS                com.tts.test         V   supported zh__#Hans
2022-10-18 15:21:22.650 25266-25266 TTS                com.tts.test         V   supported zh__#Hant
2022-10-18 15:21:22.690 25266-25266 TTS                com.tts.test         D   TTS Voice: Voice[Name: zh, locale: zh__#Hans, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS                com.tts.test         D   TTS Voice: Voice[Name: en, locale: en, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS                com.tts.test         D   TTS Voice: Voice[Name: zh, locale: zh, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.691 25266-25266 TTS                com.tts.test         D   TTS Voice: Voice[Name: zh, locale: zh__#Hant, quality: 300, latency: 300, requiresNetwork: false, features: []]
2022-10-18 15:21:22.698 25266-25266 TTS                com.tts.test         D   TTS Engine = tw.pu.tts
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注: TTS的功能需要在TextToSpeech.OnInitListener回调, 并确认状态正常后才可以开始正常工作

完整代码TTS.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;

import java.util.Locale;
import java.util.Set;

public class TTS implements TextToSpeech.OnInitListener {
    final String TAG = "TTS";
    TextToSpeech mTts;

    public TTS(Context context){
        mTts = new TextToSpeech(context, this);
    }

    public TTS(Context context, TextToSpeech.OnInitListener lis){
        mTts = new TextToSpeech(context, this);
        setOnInitListener(lis);
    }

    final int DATA_CHECK_CODE = 0x774;
    @Deprecated
    public TTS(Activity activity){
        Intent checkIntent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        activity.startActivityForResult(checkIntent, 0x774);
    }
    @Deprecated
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        if (requestCode == DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                mTts = new TextToSpeech(activity, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                activity.startActivity(installIntent);
            }
        }
    }

    public TextToSpeech getTts(){return mTts;}

    private void dump(){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Set<Locale> ava = mTts.getAvailableLanguages();
            if (ava != null) {
                for (Locale l : ava) {
                    Logger.v(TAG, "supported " + l.toString());
                }
                //mTts.setLanguage(Locale.US);
            } else {
                Logger.d(TAG, "no supported language");
            }
            Set<Voice> voices = mTts.getVoices();
            if(voices != null){
                for(Voice v : voices){
                    Logger.d(TAG, "TTS Voice: " + v.toString());
                }
            }
        }
        String engine = mTts.getDefaultEngine();
        Logger.d(TAG, "TTS Engine = " + engine);
    }

    public void setSpeechSpeed(float speed){
        mTts.setSpeechRate(speed);
    }
    public void setVoiceFreq(float freq){
        mTts.setPitch(freq);
    }

    public void speak(String s){
        if(mTts == null){
            Logger.e(TAG, "speak failed: TTS not ready");
            return;
        }
        int res = mTts.speak(s, TextToSpeech.QUEUE_ADD, null);
        if(res != 0){
            Logger.w(TAG, "speak failed: " + res);
        }else{
            Logger.d(TAG, "speak " + s + " done");
        }
    }

    public void stop(){
        mTts.stop();
    }

    @Override
    public void onInit(int status) {
        Logger.d(TAG, "onInit " + status);
        if(status == 0) {
            dump();
            mTts.setPitch(1f);
            mTts.setSpeechRate(1f);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes attr = new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
                mTts.setAudioAttributes(attr);
            }
        }

        if(initLis != null)initLis.onInit(status);
    }

    TextToSpeech.OnInitListener initLis;
    public void setOnInitListener(TextToSpeech.OnInitListener initLis){
        this.initLis = initLis;
    }
}

  • 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
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

参考

Android 中文語音引擎資源
Android中使用自带TextToSpeech实现离线语音合成功能
The 7 Best Text-to-Speech Apps for Android
Android中提供的免费文字转语音功能TextToSpeech之快速入门用法(Android TTS 语音合成播报)
An introduction to Text-To-Speech in Android

附:
文字转语音(TTS) 应用 (APK)

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

闽ICP备14008679号