1. 首先,我先把代码放到下面:
1 package com.himi.speaker; 2 3 import java.util.Locale; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.Toast; 14 import android.speech.tts.TextToSpeech; 15 16 17 public class MainActivity extends Activity implements OnClickListener { 18 19 private Button btn_speak; 20 private EditText edit_input; 21 private TextToSpeech tts; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 tts = new TextToSpeech(this, ttsInitListener); 29 edit_input = (EditText) findViewById(R.id.edit_input); 30 btn_speak = (Button) findViewById(R.id.btn_speak); 31 32 btn_speak.setOnClickListener(this); 33 } 34 35 @Override 36 public boolean onCreateOptionsMenu(Menu menu) { 37 // Inflate the menu; this adds items to the action bar if it is present. 38 getMenuInflater().inflate(R.menu.main, menu); 39 return true; 40 } 41 42 @Override 43 public boolean onOptionsItemSelected(MenuItem item) { 44 // Handle action bar item clicks here. The action bar will 45 // automatically handle clicks on the Home/Up button, so long 46 // as you specify a parent activity in AndroidManifest.xml. 47 int id = item.getItemId(); 48 if (id == R.id.action_settings) { 49 return true; 50 } 51 if (id == R.id.version) { 52 Toast.makeText(getApplicationContext(), "这是首版本v1.0 制作者:任逍遥",Toast.LENGTH_LONG).show();; 53 } 54 if (id == R.id.exit) { 55 finish(); 56 } 57 return super.onOptionsItemSelected(item); 58 } 59 60 public void onClick(View v) { 61 62 if(edit_input.getText().length() >0 ) { 63 //朗诵输入文本 64 tts.speak(edit_input.getText().toString(), TextToSpeech.QUEUE_FLUSH,null ); 65 66 67 }else { 68 tts.speak("Nothing input,Son of a bitch", TextToSpeech.QUEUE_FLUSH,null ); 69 } 70 71 } 72 73 //实现这个接口,这是手机发声的朗诵引擎,就像汽车发动机一样,启动运行.用来前面初始化创建TextToSpeech对象的,绑定这个接口实现这个对象可以具备发声的能力 74 private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() { 75 public void onInit(int status) { 76 // 使用美式时区目前不支持中文语音数据库 77 Locale loc = new Locale("us", "", ""); 78 // 判断是否安装了美式英文朗诵Text-to-Speech Library 79 if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE) { 80 tts.setLanguage(loc); 81 } 82 83 } 84 }; 85 86 87 //OnUtteranceCompletedListener-----API15之后就废弃了 88 89 // private TextToSpeech.OnUtteranceCompletedListener ttsUtteranceCompletedListener = new TextToSpeech.OnUtteranceCompletedListener() { 90 // 91 // public void onUtteranceCompleted(String utteranceId) { 92 // // TODO 自动生成的方法存根 93 // 94 // } 95 // }; 96 97 98 protected void OnDestory() { 99 tts.shutdown(); 100 super.onDestroy(); 101 102 } 103 104 105 }
接着我附上activity_main.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.himi.speaker.MainActivity" > 7 8 <EditText 9 android:id="@+id/edit_input" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:lines="1" 13 android:hint="@string/input"/> 14 <Button 15 android:id="@+id/btn_speak" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="@string/speak"/> 19 20 21 </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 制作者:任逍遥",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)输出
设置完毕之后,手机即可发声.