2016-12-22 17 views
2

My TextToSpeechは最初の実行で正常に動作しますが、アプリケーションが「戻る」キーを使用して閉じられてから再開すると機能しません。私はのonCreateでVoiceGeneratorのインスタンスを取得Android TextToSpeech:話が失敗しました:TTSエンジンにバインドされていません

public class VoiceGenerator { 
    public TextToSpeech tts; 
    private Context context; 
    private String TAG = "Voice Generator"; 

    private static VoiceGenerator instance; 

    private VoiceGenerator(Context context){ 
     this.context = context; 
    } 

    public static VoiceGenerator getInstance(Context context){ 
     if (instance == null) { 
      instance = new VoiceGenerator(context); 
     } 

     return instance; 
    } 

    public void initializeTTS() { 
     if (tts == null) { 
      Log.d(TAG, "initialize tts"); 
      tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() { 
       @Override 
       public void onInit(int status) { 
        if (status != TextToSpeech.ERROR) { 
         Log.d(TAG, "initialize tts success"); 
         tts.setLanguage(...);       
        }  
       } 
      });  
     } 
    } 

    public void speak(){ 
     tts.speak(...) 
    } 

    public void shutdown(){ 
     if(tts != null) { 
      tts.stop(); 
      tts.shutdown(); 
      tts=null; 
      Log.d(TAG, "TTS Destroyed"); 
     } 
    } 

} 

:ONSTARTで

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    voiceGenerator = VoiceGenerator.getInstance(this); 
} 

初期化TTS:エラーは、私はTTSを扱うためのクラスを持っているERROR

あるonInitTextToSpeech: speak failed: not bound to TTS enginestatusです

@Override 
protected void onStart(){ 
    super.onStart(); 
    voiceGenerator.initializeTTS(); 
} 

そしてonDestroyでそれをシャットダウン:私が間違っているのかについて

@Override 
protected void onDestroy() { 
super.onDestroy(); 
voiceGenerator.shutdown();  
} 

任意のアイデア?

答えて

0

あなただけのOnInitが行われた後、これのOnInit()で次の操作を行い、エンジン話すを聞かせすることができます

if (status == TextToSpeech.SUCCESS) { 
    tts.speak("Hello World", TextToSpeech.QUEUE_FLUSH, null);  

    } 
+0

問題はこれで解決しないようにステータスがERRORであることです。 – suue

1

あなたはMainActivityTextToSpeech.OnInitListenerを実装する必要があります。 これは良い例です。

コード:

public class MainActivity extends ActionBarActivity implements TextToSpeech.OnInitListener{ 

    private TextToSpeech engine; 
    private EditText text; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     text = (EditText) findViewById(R.id.text); 
     engine = new TextToSpeech(this, this); 
    } 



    public void speakText(View v) { 

     String textContents = text.getText().toString(); 
     engine.speak(textContents, TextToSpeech.QUEUE_FLUSH, null, null); 

    } 

    @Override 
    public void onInit(int i) { 


     if (i == TextToSpeech.SUCCESS) { 
      //Setting speech Language 
      engine.setLanguage(Locale.ENGLISH); 
      engine.setPitch(1); 
     } 
    } 

} 
関連する問題