1、概述
TextToSpeech,就是將文本內容轉換成語音,在其餘的一些應用中常常能夠看到。這個功能仍是挺強大的,可是用戶利用它來編寫應用卻很簡單。
2、要求
可以將文本內容轉換成語音並朗讀出來;能夠一次所有朗讀出來,也能夠邊寫邊讀;能夠將文本保存爲語音文件。
3、實現
新建工程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"/>
好了,運行該程序: