一、直接上代码:
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;@Overrideprotected 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() {@Overridepublic 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();}@Overridepublic 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="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid: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" /><EditTextandroid: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" /><Buttonandroid: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支持的一些语言环境:
2、TextToSpeech类中的一些其他方法:
3、播放的声音可在手机:设置->语言与输入法->文字转语音(TTS)输出 中进行设置,或安装其他平台语音识别模块并在此配置。