Android中自动朗读(TTS)的简单使用

来源:互联网 发布:手机运行电脑软件 编辑:程序博客网 时间:2024/06/10 20:16

   1、 Android支持对指定文本内容进行朗读,从而发出声音,而且还支持把文本对应的音频录制成音频文件,方便以后播放。这种自动朗读支持的类为TextToSpeech,简称TTS。该类提供了如下一个构造器:TextToSpeech(Context context , TextToSpeech.OnInitListener listener),从构造器可以看出,当创建TextToSpeech对象时,必须提供了一个OnInitListener监听器,负责监听TextToSpeech的初始结果。

  2、 一旦获得了TextToSpeech对象之后,就可以调用TextToSpeech的setLanguage方法来设置TTS发声引擎使用的语言、国家选项。目前Android的TTS暂时不支持中文。

  3、 对TextToSpeech设置完成后,就可以朗读文本了,常用的两个方法如下:

①speak(String text,int queueMode,HashMap<String,String> params)

②synthesizeToFile(String text,HashMap<String,String>params,String filename)

上面两个方法都用于把text文字内容转换问音频,区别是speak是播放音频,synthesizeToFile是把转换得到的音频保存成声音文件。

其中,speak中的queueMode参数指定TTS发音队列模式,该参数支持如下两个常量:

   4、归纳起来,使用TTS的步骤如下:

①创建TTS对象。传入的OnInitListener监听创建是否成功。

②设置TTS所使用的语言、国家选项。通过返回值判断TTS是否支持该语言、国家选项。

③调用speak或synthesizeToFile方法

④关闭TTS,回收资源。

下面用一个简单实例来演示TTS的使用,代码如下:

Activity:

package com.lovo.speech;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.speech.tts.TextToSpeech.OnInitListener;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class SpeechTestActivity extends Activity {private TextToSpeech tts;private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 初始化TextToSpeech对象tts = new TextToSpeech(this, new OnInitListener() {@Overridepublic void onInit(int status) {// 如果装载TTS引擎成功if (status == TextToSpeech.SUCCESS) {// 设置使用美式英语朗读int result = tts.setLanguage(Locale.US);// 如果不支持所设置的语言if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE&& result != TextToSpeech.LANG_AVAILABLE) {Toast.makeText(SpeechTestActivity.this,"TTS暂时不支持这种语言的朗读!", Toast.LENGTH_LONG).show();}}}});editText = (EditText) findViewById(R.id.main_et_content);}@Overrideprotected void onDestroy() {super.onDestroy();// 关闭TextToSpeech对象if (tts != null) {tts.shutdown();}}public void click(View v) {switch (v.getId()) {case R.id.main_btn_record:// 将朗读文本的音频记录到指定文件tts.synthesizeToFile(editText.getText().toString(), null,"/mnt/sdcard/sound.wav");Toast.makeText(SpeechTestActivity.this, "声音记录成功!",Toast.LENGTH_SHORT).show();break;case R.id.main_btn_speech:// 执行朗读tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD,null);break;}}}


布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/main_et_content"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:orientation="horizontal" >        <Button            android:id="@+id/main_btn_speech"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="朗读" />        <Button            android:id="@+id/main_btn_record"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:onClick="click"            android:text="记录声音" />    </LinearLayout></LinearLayout>


 

原创粉丝点击