2016-02-08 36 views
-1

问题:说话失败:不绑定到tts engine讲失败:不绑定到TTS引擎

我实现textToSpeech功能。由于发言失败,我得到了例外:不受限于tts engine。我正在实施async taskasync task将读取mail。我想转换mail body to speech

package com.example.trynot; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import java.util.Locale; 

import com.example.trynot.MainActivity.ReadMailSample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
public class Notify extends Activity implements TextToSpeech.OnInitListener { 
/** Called when the activity is first created. */ 


public TextToSpeech tts = new TextToSpeech(MainActivity.c, Notify.this); 

public Notify() 
{ 
    System.out.println("Inside Constructor"); 
    speakOut(); 
} 

@Override 
public void onDestroy() { 
// Don't forget to shutdown tts! 
if (tts != null) { 
    tts.stop(); 
    tts.shutdown(); 
} 
super.onDestroy(); 
} 

@Override 
public void onInit(int status) { 
System.out.println("inside INIT"); 
if (status == TextToSpeech.SUCCESS) { 

    int result = tts.setLanguage(Locale.US); 
    tts.speak(MainActivity.ReadMailSample.command, TextToSpeech.QUEUE_FLUSH, null); 
    if (result == TextToSpeech.LANG_MISSING_DATA 
      || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
     Log.e("TTS", "This Language is not supported"); 
    } else { 

     speakOut(); 
    } 

} else { 
    Log.e("TTS", "Initilization Failed!"); 
} 

} 

private void speakOut() { 
    System.out.println("inside SPeak out"); 
tts.speak(MainActivity.ReadMailSample.command, TextToSpeech.QUEUE_FLUSH, null); 
} 


} 
+0

你可以发布stacktrace? – Saket

回答

1

你应该将TTS引擎实例instatiation到的onCreate,这条线:

public TextToSpeech tts = new TextToSpeech(MainActivity.c, Notify.this); 

变化:

public TextToSpeech tts; 

,并添加里面的onCreate

tts = new TextToSpeech(MainActivity.c, Notify.this); 

而 - wh的ats最重要的 - 不要在活动派生类的构造函数使用:

public Notify() 
{ 
    System.out.println("Inside Constructor"); 
    speakOut(); 
} 

应该是您的onCreate:

@Override 
protected void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    //speakOut(); // here tts is not yet initialized, call it in onInit on success 
    //tts = new TextToSpeech(MainActivity.c, Notify.this); // whats MainActivity.c? 
    tts = new TextToSpeech(this, this); 
} 
1

您的谷歌文本到语音引擎可能会禁止......一旦签上您的设置

如果它被禁用它也显示相同的错误