700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > tts文字转语音_Android文字转语音(TTS)

tts文字转语音_Android文字转语音(TTS)

时间:2023-01-07 17:42:05

相关推荐

tts文字转语音_Android文字转语音(TTS)

tts文字转语音

In this tutorial, we’ll be discussing and implementing the Text To Speech in our Android application.

We will create an android application which speaks the text entered in the EditText. Android Text To Speech is also referred to as Android TTS.

在本教程中,我们将在Android应用程序中讨论和实现“文字转语音”。

我们将创建一个Android应用程序,说出在EditText中输入的文本。 Android文字转语音也称为Android TTS。

Android文字转语音 (Android Text To Speech)

TextToSpeech as the name suggests is used to synthesize speech from the text string.

顾名思义,TextToSpeech用于从文本字符串合成语音。

You can set the language of your choice. You can set the pitch, speed as well your own speech from a custom file.

您可以设置自己选择的语言。 您可以从自定义文件中设置音高,速度以及自己的语音。

TextToSpeech needs to be initialized first. For this, you need to implement theTextToSpeech.OnInitListenerinterface and override the method:onInit.

Example:

首先需要初始化TextToSpeech。 为此,您需要实现TextToSpeech.OnInitListener接口并覆盖方法:onInit

例:

TextToSpeech tts = new TextToSpeech(this, this);

Once this is done the onInit gets triggered.

一旦完成此操作,就会触发onInit。

In this method, we check whether the feature is available on our device or not.

通过这种方法,我们检查功能是否在我们的设备上可用。

@Overridepublic void onInit(int status) {if (status == TextToSpeech.SUCCESS) {int result = tts.setLanguage(Locale.US);if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) {Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();} else {//Disable the button if any.}} else {Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();}}

How to speak the text?

文字怎么说?

tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);

The first parameter is the text that would be spoken.

第一个参数是要说的文字。

The second parameter defines that the previous input is flushed so as to begin a clean slate. Alternatively, we can use QUEUE_ADD to add the current text to the speech.

第二个参数定义刷新先前的输入,以便开始清理。 或者,我们可以使用QUEUE_ADD将当前文本添加到语音中。

The third parameter is the bundle that is passed.

第三个参数是传递的包。

Fourth is theutterance idstring.

第四是utterance id字符串。

OnUtteranceProgressListener is used to listen for the tts events: start, done, error.

OnUtteranceProgressListener用于侦听tts事件:开始,完成,错误。

In this, we can retrieve the bundle utterance string that was passed.

在这种情况下,我们可以检索已传递的语音包发声字符串。

The OnUtteranceProgressListener must be defined before the speak method is called.必须在调用语音方法之前定义OnUtteranceProgressListener。

To change the pitch and speed we can do:

要更改音调和速度,我们可以执行以下操作:

tts.setPitch(2f);tts.setSpeechRate(2f);

addSpeechmethod is used to add a custom speech instead of using the Android’s default speech.

addSpeech方法用于添加自定义语音,而不是使用Android的默认语音。

Once the speech is over, you need to stop the TTS instance in order to release the resources using thetts.shutdown()method.

演讲结束后,您需要停止TTS实例,以便使用tts.shutdown()方法释放资源。

Let’s get onto the business end where we implement TextToSpeech in our application.

让我们进入在应用程序中实现TextToSpeech的业务端。

Android TTS项目结构 (Android TTS Project Structure)

文字转语音Android代码 (Text To Speech Android Code)

The code for the activity_main.xml layout is given below:

下面给出了activity_main.xml布局的代码:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:id="@+id/enterYouText"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="8dp"android:text="Welcome to "app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:text="SPEAK TEXT"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/enterYouText" /><Buttonandroid:id="@+id/btnChangePitch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="Change Pitch"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnChangeSpeed" /><Buttonandroid:id="@+id/btnChangeSpeed"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="Change Speed"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/button" /></android.support.constraint.ConstraintLayout>

The code for MainActivity.java is given below:

MainActivity.java的代码如下:

package com.journaldev.androidtexttospeech;import android.speech.tts.TextToSpeech;import android.speech.tts.UtteranceProgressListener;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Locale;public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {Button button, btnChangePitch, btnChangeSpeed;EditText editText;private TextToSpeech tts;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tts = new TextToSpeech(this, this);button = findViewById(R.id.button);btnChangePitch = findViewById(R.id.btnChangePitch);btnChangeSpeed = findViewById(R.id.btnChangeSpeed);editText = findViewById(R.id.enterYouText);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {speakOut();}});btnChangePitch.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {tts.setPitch(0.2f);}});btnChangeSpeed.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {tts.setSpeechRate(2f);}});}@Overridepublic void onInit(int status) {if (status == TextToSpeech.SUCCESS) {int result = tts.setLanguage(Locale.US);if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED) {Toast.makeText(getApplicationContext(), "Language not supported", Toast.LENGTH_SHORT).show();} else {button.setEnabled(true);}} else {Toast.makeText(getApplicationContext(), "Init failed", Toast.LENGTH_SHORT).show();}}private void speakOut() {tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {@Overridepublic void onStart(String s) {final String keyword = s;runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Started" + keyword, Toast.LENGTH_SHORT).show();}});}@Overridepublic void onDone(String s) {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Done ", Toast.LENGTH_SHORT).show();}});}@Overridepublic void onError(String s) {runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(getApplicationContext(), "Error ", Toast.LENGTH_SHORT).show();}});}});Bundle params = new Bundle();params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");String text = editText.getText().toString();tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, "Dummy String");}@Overridepublic void onDestroy() {if (tts != null) {tts.stop();tts.shutdown();}super.onDestroy();}}

runOnUiThreadfunction.runOnUiThread函数。

The output of the above application in action is given below:

上面应用程序的输出如下:

Try pressing the pitch and speech and notice the change!

尝试按音调和语音并注意变化!

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

AndroidTextToSpeechAndroidTextToSpeech Github Project LinkGithub项目链接

翻译自: /21904/android-text-to-speech-tts

tts文字转语音

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。