android 7.0 tts文字转语音

支持中文的SDK 语音引擎下载


import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import java.util.Locale;

public class SystemTTS {
    private static final String TAG = "SystemTTS";
    private static SystemTTS instance;
    private TextToSpeech textToSpeech;
    private boolean isSupport = true;

    private SystemTTS(Context context) {
        textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    textToSpeech.setPitch(1.0f); // 设置音调
                    textToSpeech.setSpeechRate(1.0f); // 设置语速
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        isSupport = false;
                        Log.i(TAG, "系统不支持中文语音播报");
                    }
                }else {
                    Log.e(TAG, "初始化系统TTS 失败");
                }
            }
        });
    }

    public static SystemTTS getInstance(Context context) {
        if (instance == null) {
            instance = new SystemTTS(context);
        }
        return instance;
    }

    public void speak(String text) {
        if (!isSupport) {
            return;
        }
        if (textToSpeech != null) {
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    public void destroy() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        instance = null;
    }
SystemTTS.getInstance(this).speak("测试语音输出");

相关推荐

  1. android 7.0 tts文字语音

    2024-07-10 04:02:06       25 阅读
  2. Edge-TTS(文字语音工具)Html版本

    2024-07-10 04:02:06       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 04:02:06       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 04:02:06       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 04:02:06       90 阅读
  4. Python语言-面向对象

    2024-07-10 04:02:06       98 阅读

热门阅读

  1. 离线升级docker中的某个镜像——以etcd为例

    2024-07-10 04:02:06       52 阅读
  2. 将pytorch 模型封装为c++ api 例子

    2024-07-10 04:02:06       34 阅读
  3. Rust: 关于Pin以及move前后分析

    2024-07-10 04:02:06       32 阅读
  4. LVS实验

    LVS实验

    2024-07-10 04:02:06      28 阅读
  5. 【Git】取消追踪多个文件或目录

    2024-07-10 04:02:06       24 阅读
  6. 环境变量Path

    2024-07-10 04:02:06       27 阅读
  7. 数据守卫者:sklearn中的异常点检测技术

    2024-07-10 04:02:06       31 阅读
  8. 概率解码:SKlearn中模型的概率预测指南

    2024-07-10 04:02:06       28 阅读
  9. 遇到的问题汇总

    2024-07-10 04:02:06       29 阅读
  10. Oracle中CREATE FORCE VIEW的说明和例子

    2024-07-10 04:02:06       27 阅读
  11. 探索邻近奥秘:SKlearn中K-近邻(KNN)算法的应用

    2024-07-10 04:02:06       25 阅读