android mvp示例
In this tutorial you will learn about android text to speech example.
在本教程中,您将学习有关android文本语音转换示例的信息。
Android provides a class TextToSpeech that helps in converting any text into speech or voice. What I am doing in this example is, I am writing any text in textbox and on button click I am taking the text from textbox and converting it into speech using TextToSpeech class.
Android提供了TextToSpeech类,可将任何文本转换为语音或语音。 在此示例中,我要在文本框中编写任何文本,然后单击按钮,然后从Textbox中提取文本,并使用TextToSpeech类将其转换为语音。
Android文字转语音示例 (Android Text to Speech Example)
First we need to create the object of TextToSpeech class. Its constructor takes two arguments, first argument is the reference of current activity and in the second argument we have to specify the listener. The setLanguage() method is used to specify the language, it takes Locale object as argument. The Locale object can be US, UK, CHINA, etc. Finally the speech() method is used to convert the text to speech, it takes three parameters or arguments. The first argument is the text that we want to convert, second argument takes constant QUEUE_FLUSH and third argument takes null.
首先,我们需要创建TextToSpeech类的对象。 它的构造函数有两个参数,第一个参数是当前活动的引用,第二个参数中,我们必须指定侦听器。 setLanguage()方法用于指定语言,它以Locale对象作为参数。 语言环境对象可以是US , UK , CHINA等。最后,使用Speech()方法将文本转换为语音,它采用三个参数或自变量。 第一个参数是我们要转换的文本,第二个参数采用常量QUEUE_FLUSH ,第三个参数采用null 。
Below I have given the whole code for doing this. The package name is com.texttospeech.
下面,我给出了执行此操作的完整代码。 程序包名称为com.texttospeech 。
activity_main.xml (activity_main.xml)
<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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp" tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text To Speech"
android:id="@+id/btn"/>
</LinearLayout>
MainActivity.java (MainActivity.java)
package com.texttospeech;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
public class MainActivity extends Activity {
EditText text;
Button btn;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(EditText)findViewById(R.id.text);
btn=(Button)findViewById(R.id.btn);
tts=new TextToSpeech(MainActivity.this,new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status!=TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tts.speak(text.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
}
});
}
}
AndroidManifest.xml (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.texttospeech" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
So this was the simple tutorial for android text to speech example. You can comment below if you have any doubts regarding above tutorial.
因此,这是有关Android文本到语音示例的简单教程。 如果您对以上教程有任何疑问,可以在下面发表评论。
翻译自: https://www.thecrazyprogrammer.com/2015/11/android-text-to-speech-example.html
android mvp示例