Android:TextToSpeech不';我不能在安卓4.1上工作
有人知道为什么我这里的Android:TextToSpeech不';我不能在安卓4.1上工作,android,Android,有人知道为什么我这里的TextToSpeech方法在Android 4.1上不再有效了吗 在sayIt() 公共类SpeakSuperActivity扩展活动实现OnInitListener{ 私有最终静态字符串TAG=“TextToSpeech”; 受保护的静态最终语言环境defaultLocale=Locale.derman; 专用静态int TTS_DATA_CHECK=100; 受保护的TextToSpeech tts=null; 受保护布尔ttsIsInit=false; @凌驾 创建
TextToSpeech
方法在Android 4.1上不再有效了吗
在sayIt()代码>
公共类SpeakSuperActivity扩展活动实现OnInitListener{
私有最终静态字符串TAG=“TextToSpeech”;
受保护的静态最终语言环境defaultLocale=Locale.derman;
专用静态int TTS_DATA_CHECK=100;
受保护的TextToSpeech tts=null;
受保护布尔ttsIsInit=false;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//使用Intent和startActivityForResult检查TTS数据是否安装在
//在下面的activityResult(int,int,Intent)方法中返回并执行操作。
意向检查意向=新意向();
checkIntent.setAction(TextToSpeech.Engine.ACTION\u CHECK\u TTS\u数据);
startActivityForResult(检查意图、TTS_数据检查);
}
//受保护的void initTextToSpeech(){
//d(标记为“TTS initTextToSpeech()已调用”);
//意向意向=新意向(发动机动作检查TTS数据);
//startActivityForResult(意图、TTS_数据检查);
// }
ActivityResultold上受保护的void(int请求代码、int结果代码、意图数据){
if(requestCode==TTS\u数据\u检查){
Log.d(标记“onActivityResult:TTS_DATA_CHECK fork已到达”);
if(resultCode==引擎。检查语音数据){
Log.d(标签“数据已安装:Engine.CHECK_VOICE_Data_PASS fork到达”);
tts=新的TextToSpeech(这个,这个);
//tts=新的TextToSpeech(这是新的OnInitListener(){
//公共无效onInit(int状态){
//if(status==TextToSpeech.SUCCESS){
//Log.w(标记“TTS onInit()success:”);
//ttsIsInit=true;
//如果(tts.isLanguageAvailable(Locale.UK)>=0){
//Log.d(标签“英国本地可用”);
//tts.setLanguage(Locale.UK);
// }
//如果(tts.isLanguageAvailable(Locale.GERMAN)>=0){
//Log.d(标签“当地德语可用”);
//tts.setLanguage(Locale.derman);
// }
//tts.设定螺距(0.8f);
//tts.setSpeechRate(1.1f);
//说话(“哈罗·斯普吕切克洛弗”);
//}else if(status==TextToSpeech.ERROR){
//w(标记“TTS onInit()失败:(”);
// }
// }
// });
}否则{
i(标记“TTS尚未安装,调用意图安装…”);
Intent installVoice=新的Intent(Engine.ACTION\u INSTALL\u TTS\u数据);
星触觉(声音);
}
}
}
//如果设备上安装了TextToSpeech语言数据,则创建TTS实例。如果未安装
//已安装,请尝试将其安装到设备上。
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==TTS\u数据\u检查){
if(resultCode==TextToSpeech.Engine.CHECK\u VOICE\u DATA\u PASS){
//成功,因此创建TTS实例。但在
//下面定义的onInit(status)回调将运行,指示初始化。
Log.i(标记“Success,让我们谈谈”);
//XXX:注意:使用new TextToSpeech(getApplicationContext(),this)而不是new TextToSpeech(this,this)非常重要!
tts=new TextToSpeech(getApplicationContext(),this);
////使用静态区域设置方法列出设备上的可用区域设置
//Locale locales[]=Locale.getAvailableLocales();
//i(标记“设备上可用的区域设置:”);
//对于(int i=0;i=0){
Log.d(标签“英国本地可用”);
tts.setLanguage(Locale.UK);
}
如果(tts.isLanguageAvailable(Locale.GERMAN)>=0){
Log.d(标签“当地德语可用”);
tts.setLanguage(Locale.derman);
}
tts.设定螺距(0.8f);
tts.setSpeechRate(1.1f);
sayIt(“Hallo Sprücheklopfer”,真的);
}else if(状态==TextToSpeech.ERROR){
w(标记“TTS onInit()失败:(”);
}否则{
w(标记“TTS onInit()未知状态:”+状态);
}
}
}
public class SpeakSuperActivity extends Activity implements OnInitListener {
private final static String TAG = "TextToSpeech";
protected static final Locale defaultLocale = Locale.GERMAN;
private static int TTS_DATA_CHECK = 100;
protected TextToSpeech tts = null;
protected boolean ttsIsInit = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an Intent and startActivityForResult to check whether TTS data installed on the
// device. Result returned and acted on in method onActivityResult(int, int, Intent) below.
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_DATA_CHECK);
}
// protected void initTextToSpeech() {
// Log.d(TAG, "TTS initTextToSpeech() called");
// Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
// startActivityForResult(intent, TTS_DATA_CHECK);
// }
protected void onActivityResultOld(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
Log.d(TAG, "onActivityResult: TTS_DATA_CHECK fork reached");
if (resultCode == Engine.CHECK_VOICE_DATA_PASS) {
Log.d(TAG, "Data installed: Engine.CHECK_VOICE_DATA_PASS fork reached");
tts = new TextToSpeech(this, this);
// tts = new TextToSpeech(this, new OnInitListener() {
// public void onInit(int status) {
// if (status == TextToSpeech.SUCCESS) {
// Log.w(TAG, "TTS onInit() success :)");
// ttsIsInit = true;
// if (tts.isLanguageAvailable(Locale.UK) >= 0) {
// Log.d(TAG, "Local UK available");
// tts.setLanguage(Locale.UK);
// }
// if (tts.isLanguageAvailable(Locale.GERMAN) >= 0) {
// Log.d(TAG, "Local German available");
// tts.setLanguage(Locale.GERMAN);
// }
// tts.setPitch(0.8f);
// tts.setSpeechRate(1.1f);
// speak("Hallo Sprücheklopfer");
// } else if (status == TextToSpeech.ERROR) {
// Log.w(TAG, "TTS onInit() failed :(");
// }
// }
// });
} else {
Log.i(TAG, "TTS not installed yet, calling intent to install...");
Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installVoice);
}
}
}
// Create the TTS instance if TextToSpeech language data are installed on device. If not
// installed, attempt to install it on the device.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TTS_DATA_CHECK) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// Success, so create the TTS instance. But can't use it to speak until
// the onInit(status) callback defined below runs, indicating initialization.
Log.i(TAG, "Success, let's talk");
// XXX: NOTE: it is REALLY important to use new TextToSpeech(getApplicationContext(), this) instead of new TextToSpeech(this, this)!
tts = new TextToSpeech(getApplicationContext(), this);
// // Use static Locales method to list available locales on device
// Locale locales [] = Locale.getAvailableLocales();
// Log.i(TAG,"Locales Available on Device:");
// for(int i=0; i<locales.length; i++){
// String temp = "Locale "+i+": "+locales[i]+" Language="
// +locales[i].getDisplayLanguage();
// if(locales[i].getDisplayCountry() != "") {
// temp += " Country="+locales[i].getDisplayCountry();
// }
// Log.i(TAG, temp);
// }
} else {
// missing data, so install it on the device
Log.i(TAG, "Missing Data; Install it");
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsIsInit = true;
// Set to a language locale after checking availability
Log.i(TAG, "defaultLocaleAvailable=" + tts.isLanguageAvailable(defaultLocale));
tts.setLanguage(defaultLocale);
// Examples of voice controls. Set to defaults of 1.0.
tts.setPitch(1.0F);
tts.setSpeechRate(1.0F);
// Issue a greeting and instructions in the default language
// speakGreeting(defaultLocale.getDisplayLanguage());
// sayIt("Hallo Sprücheklopfer", true);
} else {
ttsIsInit = false;
Log.i(TAG, "Failure: TextToSpeech instance tts was not properly initialized");
}
}
// protected void speak(String text) {
// Log.d(TAG, "TTS sould say: " + text);
// if (tts != null && ttsIsInit) {
// tts.speak(text, TextToSpeech.QUEUE_ADD, null);
// } else {
// Log.w(TAG, "TTS not initialised or null");
// }
// }
// Method to speak a string. The boolean flushQ determines whether the text is
// appended to the queue (if false), or if the queue is flushed first (if true).
public void sayIt(String text, boolean flushQ) {
if (tts != null && ttsIsInit) {
if (flushQ) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
} else {
Log.i(TAG, "Failure: TextToSpeech instance tts was not properly initialized");
}
}
// Release TTS resources when finished
@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
ttsIsInit = false;
}
super.onDestroy();
}
public void onInitOld(int status) {
Log.i(TAG, "TTS onInit() status :" + status);
if (status == TextToSpeech.SUCCESS) {
Log.w(TAG, "TTS onInit() success :)");
ttsIsInit = true;
if (tts.isLanguageAvailable(Locale.UK) >= 0) {
Log.d(TAG, "Local UK available");
tts.setLanguage(Locale.UK);
}
if (tts.isLanguageAvailable(Locale.GERMAN) >= 0) {
Log.d(TAG, "Local German available");
tts.setLanguage(Locale.GERMAN);
}
tts.setPitch(0.8f);
tts.setSpeechRate(1.1f);
sayIt("Hallo Sprücheklopfer", true);
} else if (status == TextToSpeech.ERROR) {
Log.w(TAG, "TTS onInit() failed :(");
} else {
Log.w(TAG, "TTS onInit() unknown status :" + status);
}
}
}