1. 首先,我先把代码放到下面:
package com.himi.speaker; import java.util.Locale; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.speech.tts.TextToSpeech; public class MainActivity extends Activity implements OnClickListener { private Button btn_speak;
private EditText edit_input;
private TextToSpeech tts; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tts = new TextToSpeech(this, ttsInitListener);
edit_input = (EditText) findViewById(R.id.edit_input);
btn_speak = (Button) findViewById(R.id.btn_speak); btn_speak.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.version) {
Toast.makeText(getApplicationContext(), "这是首版本v1.0\n制作者:任逍遥",Toast.LENGTH_LONG).show();;
}
if (id == R.id.exit) {
finish();
}
return super.onOptionsItemSelected(item);
} public void onClick(View v) { if(edit_input.getText().length() >0 ) {
//朗诵输入文本
tts.speak(edit_input.getText().toString(), TextToSpeech.QUEUE_FLUSH,null ); }else {
tts.speak("Nothing input,Son of a bitch", TextToSpeech.QUEUE_FLUSH,null );
} } //实现这个接口,这是手机发声的朗诵引擎,就像汽车发动机一样,启动运行.用来前面初始化创建TextToSpeech对象的,绑定这个接口实现这个对象可以具备发声的能力
private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {
public void onInit(int status) {
// 使用美式时区目前不支持中文语音数据库
Locale loc = new Locale("us", "", "");
// 判断是否安装了美式英文朗诵Text-to-Speech Library
if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE) {
tts.setLanguage(loc);
} }
}; //OnUtteranceCompletedListener-----API15之后就废弃了 // private TextToSpeech.OnUtteranceCompletedListener ttsUtteranceCompletedListener = new TextToSpeech.OnUtteranceCompletedListener() {
//
// public void onUtteranceCompleted(String utteranceId) {
// // TODO 自动生成的方法存根
//
// }
// }; protected void OnDestory() {
tts.shutdown();
super.onDestroy(); } }
接着我附上activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.himi.speaker.MainActivity" > <EditText
android:id="@+id/edit_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:hint="@string/input"/>
<Button
android:id="@+id/btn_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/speak"/> </LinearLayout>
下图是在虚拟机上运行效果图的:
这里发现我在虚拟机上可以运行,比如输入"I love you",确实发出英文朗读,但是我把这段程序下载到手机真机上就不能实现朗诵了,
我猜想是真机没有安装这里的TextToSpeech Library这个语言数据库文件,后来我就修改了一段程序试着验证我的猜想:如下:
package com.himi.speaker; import java.util.Locale; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.speech.tts.TextToSpeech; public class MainActivity extends Activity implements OnClickListener { private Button btn_speak;
private EditText edit_input;
private TextToSpeech tts; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tts = new TextToSpeech(this, ttsInitListener);
edit_input = (EditText) findViewById(R.id.edit_input);
btn_speak = (Button) findViewById(R.id.btn_speak); btn_speak.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.version) {
Toast.makeText(getApplicationContext(), "这是首版本v1.0\n制作者:任逍遥",Toast.LENGTH_LONG).show();;
}
if (id == R.id.exit) {
finish();
}
return super.onOptionsItemSelected(item);
} public void onClick(View v) { if(edit_input.getText().length() >0 ) {
//朗诵输入文本
tts.speak(edit_input.getText().toString(), TextToSpeech.QUEUE_FLUSH,null ); }else {
tts.speak("Nothing input,Son of a bitch", TextToSpeech.QUEUE_FLUSH,null );
} } //实现这个接口,这是手机发声的朗诵引擎,就像汽车发动机一样,启动运行.用来前面初始化创建TextToSpeech对象的,绑定这个接口实现这个对象可以具备发声的能力
private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {
public void onInit(int status) {
// 使用美式时区目前不支持中文语音数据库
Locale loc = new Locale("us", "", "");
// 判断是否安装了美式英文朗诵Text-to-Speech Library
if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE)
{
tts.setLanguage(loc);
}
else //如果我的手机没有安装语音数据,程序点击进入之后,就会自动结束
{
finish();
} }
}; //OnUtteranceCompletedListener-----API15之后就废弃了 // private TextToSpeech.OnUtteranceCompletedListener ttsUtteranceCompletedListener = new TextToSpeech.OnUtteranceCompletedListener() {
//
// public void onUtteranceCompleted(String utteranceId) {
// // TODO 自动生成的方法存根
//
// }
// }; protected void OnDestory() {
tts.shutdown();
super.onDestroy(); } }
事实上,我的在真机上,点击进入之后,确实是直接退出程序。
2. 于是,我在手机(小米3)中安装了:SpeechSynthesis Data这个语音数据,安装完了之后
进入:设置 / 全部设置 / 辅助功能 / 文字转语音(TTS)输出
设置完毕之后,手机即可发声.