Android file-to-speech TTS output (text-to-speech)

1. Introduction

Table of contents

1. Introduction

Two, configuration

Three, use

4. Authority

5. Supplement


At present, the domestic Android system's built-in voice engine is basically Pico TTS, and Pico TTS does not support Chinese-to-speech broadcasting. Because the project needs to play Chinese, the HKUST Xunfei Engine 3.0.apk is introduced.

Two, configuration

Download HKUST Xunfei Speech Engine 3.0.apk (if you download Xiaoai, Xiaodu and other engines, you can also use it), after the installation is successful, enter System Settings -> Language Input Method Settings -> Text-to-Speech (TTS) Output -> Select HKUST Xunfei speech engine (choose the engine you downloaded).

Three, use

The code is very simple, the comments are very detailed, don't talk nonsense, go directly to the tool class.

package com.welbell.temperature.type.ui;

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

import java.util.Locale;

public class VoiceAnnouncements implements TextToSpeech.OnInitListener {

    private static final String TAG = VoiceAnnouncements.class.getName();
    public static VoiceAnnouncements mVoiceAnnouncements;
    private TextToSpeech mTextToSpeech;

    public static VoiceAnnouncements getInstance() {
        synchronized (VoiceAnnouncements.class) {
            if (mVoiceAnnouncements == null) {
                mVoiceAnnouncements = new VoiceAnnouncements();
            }
        }
        return mVoiceAnnouncements;
    }

    public void init(Context context) {
        mTextToSpeech = new TextToSpeech(context, this);
    }

    /**
     * 设置语音播放参数(设置-1将不起作用)
     *
     * @param pitch      音调
     * @param speechRate 语速
     */
    public void setParameter(float pitch, float speechRate) {
        if (mTextToSpeech == null) {
            return;
        }
        if (pitch != -1)
            //设置音调
            mTextToSpeech.setPitch(pitch);
        if (speechRate != -1)
            //设置语速,1.0为正常语速
            mTextToSpeech.setSpeechRate(speechRate);
    }

    /**
     * 设置播放内容
     * 播放语音前需要等初始化成功之后 
     * @param isCover 是否覆盖式播放
     * @param text  播放的内容
     */
    public void speak(boolean isCover,String text){
        if (isCover){
            mTextToSpeech.stop();
        }
        mTextToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
    }

    /**
     * 回调函数
     * @param status
     */
    @Override
    public void onInit(int status) {
            //设置朗读语言
        int supported = mTextToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
        if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
            Log.e(TAG, "onInit: 不支持当前语言");
        }
    }
}

4. Authority

TTS needs to use file operation permission

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 外部存储读权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 外部存储写权限 -->

5. Supplement

The third-party engine APK needs to be manually set as the default TTS output by the user, otherwise it cannot be used. At present, apart from changing the framework layer, there is no way for the upper Android layer to modify the third-party APK as the default output TTS application.

 

Guess you like

Origin blog.csdn.net/duanchuanzhi/article/details/126101677