目录
一、TextToSpeech类中的常用方法
二、使用例子
MainActivity:
activity_main:
效果图:
一、TextToSpeech类中的常用方法
TextToSpeech是一个Android平台提供的文本转语音功能,可以将文字快速地转换为语音播放。
方法名称 | 功能 |
setLanguage(Locale) | 设置要输出的语言种类 |
setPitch(float x) | 设置语音的音调(范围0.5到2.0) |
setSpeechRate(float) | 设置语音的速度(范围为0.1到2.0) |
speak(CharSequence c,int i,Bundle b,String s) | 将文本转成语音并播放 |
stop() | 停止当前正在播放的语音 |
shutdown() | 释放资源 |
二、使用例子
MainActivity:
package com.example.tts;
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 android.widget.Toast;
import java.io.File;
import java.util.Locale;
public class MainActivity extends Activity implements View.OnClickListener {private TextToSpeech tts;private EditText editText;Button speak,record;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化TextToSpeech对象initToSpeech();// 初始化界面initView();}private void initView() {editText = (EditText) findViewById(R.id.text_ed);speak = findViewById(R.id.speak);speak.setOnClickListener(this);record = findViewById(R.id.record_voice);record.setOnClickListener(this);}private void initToSpeech() {tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {@Overridepublic void onInit(int status) {// 如果装载TTS引擎成功if (status == TextToSpeech.SUCCESS) {// 设置使用中文朗读int result = tts.setLanguage(Locale.CHINA);// 如果不支持所设置的语言if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE&& result != TextToSpeech.LANG_AVAILABLE) {Toast.makeText(MainActivity.this,"TTS暂时不支持这种语言的朗读!", Toast.LENGTH_LONG).show();}}}});tts.setPitch(1.5f);// 设置语速tts.setSpeechRate(1.0f);}@Overrideprotected void onDestroy() {super.onDestroy();// 关闭TextToSpeech对象if (tts != null) {tts.shutdown();}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.record_voice:// 将朗读文本的音频记录到指定文件File f1=new File("/mnt/sdcard/sound.wav");//Android 5.0及以上tts.synthesizeToFile (editText.getText(), null, f1,"111");//Android 5.0 以下//tts.synthesizeToFile(editText.getText().toString(), null, "/mnt/sdcard/sound.wav");Toast.makeText(MainActivity.this, "声音记录成功!",Toast.LENGTH_SHORT).show();break;case R.id.speak:// 执行朗读//Android 5.0及以上
// tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null, "111");//Android 5.0 以下tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);break;}}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:id="@+id/text_ed"android:layout_width="match_parent"android:layout_height="wrap_content"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/speak"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="朗读"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/record_voice"android:hint="11111"android:text="记录语音"/>
</LinearLayout></LinearLayout>
效果图: