1. Directly upload the code:
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>
2. Effect: Input 'Chinese' and click the button to pronounce
3. Compatible with higher versions of Android
If there is no sound when switching to voice in Android 11, and an error is reported: speak failed: not bound to TTS engine , you need to declare the following content in the AndroidManifest.xml file:
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE"/>
</intent>
</queries>
4. Expansion
1. Some locales supported by setLanguage:
language | constant |
---|---|
American English | US |
Canadian French | CANADA_FRENCH |
German | GERMANY |
italian | ITALY |
Japanese | JAPAN |
Chinese | CHINA |
2. Some other methods in the TextToSpeech class:
method | illustrate |
---|---|
addSpeech(String text, String filename) | This method adds a mapping between text strings and sound files. |
getLanguage() | This method returns a Locale instance describing the language. |
isSpeaking() | This method checks to see if the TextToSpeech engine is busy speaking. |
setPitch(float pitch) | This method sets the speech pitch of the TextToSpeech engine. |
setSpeechRate(float speechRate) | This method sets the speech rate. |
shutdown() | This method releases resources used by the TextToSpeech engine. |
stop() | This method stops talking. |
3. The sound played can be set in the mobile phone: Settings -> Language and Input Method -> Text-to-Speech (TTS) output, or install other platform speech recognition modules and configure them here.
Application Name | support offline | Remark | download link |
---|---|---|---|
ITRI TTS | yes | none | download |
Xunfei Yuji | yes | You need to open it once, but you don't need to log in | download |
HKUST Xunfei Speech Engine 3.0 | yes | recommend | download |
Speech Services by Google | no | You need to surf the Internet scientifically, and you need to download the voice pack first when offline | download |