• Android应用开发提高篇(2)文本朗读TTS(TextToSpeech)


    一、概述

          TextToSpeech,就是将文本内容转换成语音,在其他的一些应用中经常可以看到。这个功能还是挺强大的,但是用户利用它来编写应用却很简单。


    二、要求

         能够将文本内容转换成语音并朗读出来;可以一次全部朗读出来,也可以边写边读;可以将文本保存为语音文件。


    三、实现

         新建工程MySpeak,修改/res/layout/main.xml文件,在里面添加一个EditText,两个Button和一个CheckBox,完整的main.xml文件如下:

     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:layout_width="fill_parent"
    4 android:layout_height="fill_parent"
    5 android:orientation="vertical" >
    6
    7 <EditText
    8 android:id="@+id/edittext"
    9 android:layout_width="fill_parent"
    10 android:layout_height="wrap_content"
    11 />
    12
    13 <Button
    14 android:id="@+id/rbutton"
    15 android:layout_width="fill_parent"
    16 android:layout_height="wrap_content"
    17 android:text="朗读"
    18 />
    19
    20 <Button
    21 android:id="@+id/sbutton"
    22 android:layout_width="fill_parent"
    23 android:layout_height="wrap_content"
    24 android:text="保存"
    25 />
    26
    27 <CheckBox
    28 android:id="@+id/checkbox"
    29 android:layout_width="fill_parent"
    30 android:layout_height="wrap_content"
    31 android:text="边写边读"
    32 android:checked="true"
    33 />
    34
    35
    36 </LinearLayout>

    修改MySpeakActivity.java文件,设置两个Button按钮的监听和EditText的内容变化监听,完整的MySpeakActivity.java内容如下:

      1 package com.nan.speak;
    2
    3 import java.util.Locale;
    4
    5 import android.app.Activity;
    6 import android.os.Bundle;
    7 import android.speech.tts.TextToSpeech;
    8 import android.text.Editable;
    9 import android.text.TextWatcher;
    10 import android.view.View;
    11 import android.widget.Button;
    12 import android.widget.CheckBox;
    13 import android.widget.EditText;
    14 import android.widget.Toast;
    15
    16
    17 public class MySpeakActivity extends Activity
    18 {
    19 private EditText mEditText = null;
    20 private Button readButton = null;
    21 private Button saveButton = null;
    22 private CheckBox mCheckBox = null;
    23 private TextToSpeech mTextToSpeech = null;
    24
    25 /** Called when the activity is first created. */
    26 @Override
    27 public void onCreate(Bundle savedInstanceState)
    28 {
    29 super.onCreate(savedInstanceState);
    30 setContentView(R.layout.main);
    31
    32 mEditText = (EditText)this.findViewById(R.id.edittext);
    33 readButton = (Button)this.findViewById(R.id.rbutton);
    34 saveButton = (Button)this.findViewById(R.id.sbutton);
    35 mCheckBox = (CheckBox)this.findViewById(R.id.checkbox);
    36 //实例并初始化TTS对象
    37 mTextToSpeech = new TextToSpeech(this,new TextToSpeech.OnInitListener()
    38 {
    39
    40 @Override
    41 public void onInit(int status)
    42 {
    43 // TODO Auto-generated method stub
    44 if(status == TextToSpeech.SUCCESS)
    45 {
    46 //设置朗读语言
    47 int supported = mTextToSpeech.setLanguage(Locale.US);
    48 if((supported != TextToSpeech.LANG_AVAILABLE)&&(supported != TextToSpeech.LANG_COUNTRY_AVAILABLE))
    49 {
    50 displayToast("不支持当前语言!");
    51 }
    52 }
    53 }
    54
    55 });
    56 //朗读按钮监听
    57 readButton.setOnClickListener(new View.OnClickListener()
    58 {
    59
    60 @Override
    61 public void onClick(View v)
    62 {
    63 // TODO Auto-generated method stub
    64 //朗读EditText里的内容
    65 mTextToSpeech.speak(mEditText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
    66 }
    67 });
    68 //保存按钮监听
    69 saveButton.setOnClickListener(new View.OnClickListener()
    70 {
    71
    72 @Override
    73 public void onClick(View v)
    74 {
    75 // TODO Auto-generated method stub
    76
    77 //将EditText里的内容保存为语音文件
    78 int r = mTextToSpeech.synthesizeToFile(mEditText.getText().toString(), null, "/mnt/sdcard/speak.wav");
    79 if(r == TextToSpeech.SUCCESS)
    80 displayToast("保存成功!");
    81 }
    82 });
    83 //EditText内容变化监听
    84 mEditText.addTextChangedListener(mTextWatcher);
    85
    86 }
    87
    88
    89 private TextWatcher mTextWatcher = new TextWatcher()
    90 {
    91
    92 @Override
    93 public void afterTextChanged(Editable s)
    94 {
    95 // TODO Auto-generated method stub
    96 //如果是边写边读
    97 if(mCheckBox.isChecked()&&(s.length()!=0))
    98 {
    99 //获得EditText的所有内容
    100 String t = s.toString();
    101 mTextToSpeech.speak(t.substring(s.length()-1), TextToSpeech.QUEUE_FLUSH, null);
    102 }
    103 }
    104
    105 @Override
    106 public void beforeTextChanged(CharSequence s, int start, int count,
    107 int after)
    108 {
    109 // TODO Auto-generated method stub
    110
    111 }
    112
    113 @Override
    114 public void onTextChanged(CharSequence s, int start, int before,
    115 int count)
    116 {
    117 // TODO Auto-generated method stub
    118
    119 }
    120 };
    121
    122 //显示Toast函数
    123 private void displayToast(String s)
    124 {
    125 Toast.makeText(MySpeakActivity.this, s, Toast.LENGTH_SHORT).show();
    126 }
    127
    128
    129 @Override
    130 public void onDestroy()
    131 {
    132 super.onDestroy();
    133
    134 if(mTextToSpeech != null)
    135 mTextToSpeech.shutdown();//关闭TTS
    136 }
    137
    138 }

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

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

    好了,运行该程序:

    输入“123456789”,可以听到输入完一个字就马上被朗读出来,

    说明:

        不知道为什么,在我的一台真机上测试时不能朗读出来,提示说语言不支持,在另一台上可以。

  • 相关阅读:
    MathML
    Redux counterpart rematch dva
    flow
    pauseable 库
    a simple machine learning system demo, for ML study.
    react图工具集成
    SQLite
    Celery
    RabbitMQ installation
    Thunk
  • 原文地址:https://www.cnblogs.com/lknlfy/p/2368696.html
Copyright © 2020-2023  润新知