Android自动朗读(TTS)的实现

前言: Android提供了自动朗读支持。可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,方便以后播放。Android的自动朗读主要通过TextToSpeech来完成,构造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);当创建TextToSpeech对象时,必须先提供一个OnInitListener监听器——负责监听TextToSpeech的初始化结果。

效果图如下:

使用TextToSpeech的步骤如下:

  1. 创建TextToSpeech对象,创建时传入OnInitListener监听器监听示范创建成功。
  2. 设置TextToSpeech所使用语言国家选项,通过返回值判断TTS是否支持该语言、国家选项。
  3. 调用speak()或synthesizeToFile方法。
  4. 关闭TTS,回收资源。

布局文件:

<?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">    <EditText        android:id="@+id/input_text"        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <LinearLayout        android:layout_marginTop="10dp"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/speech"            android:text="Speech"            android:layout_width="wrap_content"            android:layout_weight="1"            android:layout_height="wrap_content"/>        <Button            android:id="@+id/record"            android:text="Record"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>    LinearLayout>LinearLayout>

Activity文件

public class SpeechActivity extends AppCompatActivity {    private EditText input;    private Button speech,record;    private TextToSpeech textToSpeech;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_speech);        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {            @Override            public void onInit(int status) {                if (status == textToSpeech.SUCCESS) {                    int result = textToSpeech.setLanguage(Locale.CHINA);                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE                            && result != TextToSpeech.LANG_AVAILABLE){                        Toast.makeText(SpeechActivity.this, "TTS暂时不支持这种语音的朗读!",                                Toast.LENGTH_SHORT).show();                    }                }            }        });        input = (EditText) findViewById(R.id.input_text);        speech = (Button) findViewById(R.id.speech);        record = (Button) findViewById(R.id.record);        speech.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                textToSpeech.speak(input.getText().toString(),                        TextToSpeech.QUEUE_ADD, null);            }        });        record.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String inputText = input.getText().toString();                HashMap myHashRender = new HashMap<>();                myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);                textToSpeech.synthesizeToFile(inputText, myHashRender,                        "/mnt/sdcard/my_recorder_audios/sound.wav");                Toast.makeText(SpeechActivity.this, "声音记录成功。", Toast.LENGTH_SHORT).show();            }        });    }    @Override    protected void onDestroy() {        if (textToSpeech != null)            textToSpeech.shutdown();        super.onDestroy();    }}

这里我们使用的是中文,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根据自己的需求更改为其他支持的语言。

最后在AndroidManifest.xml中加入权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

总结:通过使用Android提供的TTS,我们可以对指定文本内容进行朗读,从而发生声音;还允许把文本对应的音频录制成音频文件,保存到本地。

更多相关文章

  1. Android——EditText【输入框】的所有属性和常用属性归纳
  2. 1.3TextView
  3. Android布局的一些属性和开关、创建log图片
  4. 推荐一个android学习网站
  5. Android(安卓)EditText属性总结
  6. Android开发环境搭建流程
  7. Android用户界面 UI组件--TextView及其子类(一) TextView
  8. 推荐一个android学习网站
  9. Android深入理解Context--Service中Context的创建过程

随机推荐

  1. Android(安卓)UI--ViewPager扩展Tab标签
  2. Android(安卓)实现自定义安全数字键盘(仿
  3. Binder框架在Framework层的C++中的使用
  4. Android(安卓)中px, pd, sp 概念以及如何
  5. 平安好医生、春雨医生、丁香医生三款产品
  6. 绿色守护-锁屏后干掉指定程序,让 Android(
  7. 如何删除Android系统中的内置应用
  8. [Android] View 中事件的传递
  9. 如何解决夜神模拟器连不上adb的问题
  10. Android虚拟机DVM和JAVA虚拟机JVM的区别