zoukankan      html  css  js  c++  java
  • Android应用开发提高篇(2)-----文本朗读TTS(TextToSpeech)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/26/2368696.html

    一、概述

          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”,可以听到输入完一个字就马上被朗读出来,

    说明:

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

    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    06.数组模拟栈-简易计算器
    05.单向环形链表应用 -- 约瑟夫环
    02.数组模拟环形队列
    01.稀疏矩阵与二维数组相互转化
    大数字相乘
    13.打包发布
    悦苗园公益活动
    程序猿打招自己的电子图书馆
    【技能】提高网站可用性
    【微信技能】如何通过微信号知道对方微信的二维码
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/5179035.html
Copyright © 2011-2022 走看看