Android通过TextToSpeech实现文字转语音

一、直接上代码:

import android.app.Activity;
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 java.util.Locale;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener {
    TextToSpeech textToSpeech;
    EditText ed1;
    Button b1;
    String toSpeak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1 = findViewById(R.id.editText);
        b1 = findViewById(R.id.button);

        textToSpeech = new TextToSpeech(getApplicationContext(), this);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSpeak = ed1.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
//                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_ADD, null, null);
            }
        });
    }


    public void onPause() {
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onPause();
    }

    @Override
    public void onInit(int status) {
        //判断是否转化成功
        if (status == TextToSpeech.SUCCESS) {
            //设置语言为中文
            int languageCode = textToSpeech.setLanguage(Locale.CHINA);
            //判断是否支持这种语言,Android原生不支持中文,使用科大讯飞的tts引擎就可以了
            if (languageCode == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.d("TAG", "onInit: 不支持这种语言");
            } else {
                //不支持就改成英文
                textToSpeech.setLanguage(Locale.US);
            }
            //设置音调,值越大声音越尖(女生),值越小则变成男声,1.0是常规
            textToSpeech.setPitch(1.0f);
            //设置语速
            textToSpeech.setSpeechRate(1.0f);
            //在onInIt方法里直接调用tts的播报功能
//            textToSpeech.speak("李佩伦打卡成功", TextToSpeech.QUEUE_ADD, null);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="文字转语音示例"
        android:textSize="35sp" />


    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="46dp"
        android:hint="输入文字"
        android:textColor="#ff7aff10"
        android:textColorHint="#ffff23d1" />

    <Button
        android:id="@+id/button"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="文字转语音" />

</RelativeLayout>

二、效果:输入‘中文’点击按钮发音

 三、Android高版本兼容

        如果在Android11里转语音不发声,并且报错:speak failed:not bound to TTS engine,则需要在AndroidManifest.xml文件中声明如下内容:

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

四、扩展

1、setLanguage支持的一些语言环境:

语言 常量
美式英语 US
加拿大法语 CANADA_FRENCH
德语 GERMANY
意大利语 ITALY
日语 JAPAN
汉语 CHINA

 2、TextToSpeech类中的一些其他方法:

方法 说明
addSpeech(String text, String filename) 此方法在文本字符串和声音文件之间添加映射。
getLanguage() 此方法返回描述语言的Locale实例。
isSpeaking() 此方法检查TextToSpeech引擎是否正在忙于讲话。
setPitch(float pitch) 此方法设置TextToSpeech引擎的语音音调。
setSpeechRate(float speechRate) 此方法设置语音速率。
shutdown() 此方法释放TextToSpeech引擎使用的资源。
stop() 这种方法停止说话。

3、播放的声音可在手机:设置->语言与输入法->文字转语音(TTS)输出 中进行设置,或安装其他平台语音识别模块并在此配置。

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

猜你喜欢

转载自blog.csdn.net/weixin_43192102/article/details/130881028