Android原生的TTS(语音播报功能)

11,832 阅读2分钟

「我参与2022首次更文挑战的第7天,活动详情查看:2022首次更文挑战

引言

TTS(从文本到语音,Text To Speech),是将文本转化为语音,人机对话的一部分,让机器能够说话。
android自带的TTS目前只支持英文、法文、意大利文、德文、西班牙文。不支持中文,如果想要播放出中文语音需要借助中文TTS引擎的帮助。面向 Android 11使用文本转语音的应用应先在AndroidManifest.xml声明INTENT_ACTION_TTS_SERVICE

<queries>
  ...
 <intent>
    <action android:name="android.intent.action.TTS_SERVICE" />
 </intent>
</queries>

TextToSpeech介绍

1.构造方法:

  • TextToSpeech(Context context, OnInitListener listener):参数为上下文,初始化监听接口。使用默认的 TTS 引擎。
  • TextToSpeech(Context context, OnInitListener listener, String engine):参数为上下文,初始化监听接口,使用给定的 TTS 引擎。 2.textToSpeech.setLanguage():设置播报的语言

3.textToSpeech.speak():播放

4.textToSpeech.setPitch():设置语调,值越大声音越尖锐,越小声音越低沉

5.textToSpeech.setSpeechRate():设置语速,较低的值会减慢语音(0.5是正常语速的一半),更大的值会加速它(2.0是正常语速的两倍)

简单的android原生TTS开发

1.添加权限(读写权限)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.xwr.ndktest.TTSActivity">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="文本输入" />

    <Button
        android:id="@+id/btn_speech"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="speech" />

</LinearLayout>

2.初始化textToSeepch

package com.xwr.ndktest;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

public class TTSActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    private static final String TAG = "TTSActivity";

    private TextToSpeech textToSpeech;

    private EditText inputEt;

    private Button speechBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tts);
        speechBtn = findViewById(R.id.btn_speech);
        inputEt = findViewById(R.id.et_input);
        init();
    }

    private void init() {
        textToSpeech = new TextToSpeech(this, this);
        //设置语言
        int result = textToSpeech.setLanguage(Locale.ENGLISH);
        if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                && result != TextToSpeech.LANG_AVAILABLE) {
            Toast.makeText(TTSActivity.this, "TTS暂时不支持这种语音的朗读!",
                    Toast.LENGTH_SHORT).show();
        }
        //设置音调
        textToSpeech.setPitch(1.0f);
        //设置语速,1.0为正常语速
        textToSpeech.setSpeechRate(1.5f);
        //speech按钮监听事件
        speechBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //播放
                textToSpeech.speak(inputEt.getText().toString(),
                        TextToSpeech.QUEUE_ADD, null);
            }
        });
    }

    @Override
    public void onInit(int status) {
        //初始化成功
        if (status == TextToSpeech.SUCCESS) {
            Log.d(TAG, "init success");
        } else {
            Log.d(TAG, "init fail");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //中断当前话语
        textToSpeech.stop();
        //释放资源
        textToSpeech.shutdown();
    }
}

题外话:更羡慕街边咖啡座里的目光,只一闪,便觉得日月悠长、山河无恙。 --《行者无疆》