2011-11-25 130 views
0

我在Service中遇到了TTS的問題。它的行爲就像它想說話但它從來沒有。看着它打印的「收到的TTS:它應該說的文本」LogCat,當它初始化時就會記錄並顯示成功。我試圖爲它創建一個線程,但沒有幫助。 onUtteranceComplete從來沒有觸發過。我甚至做過這樣的(只是用於測試)while循環:TextToSpeech in a服務

while(mTTS.isSpeaking()) { 
     Log.d("", "speaking"); 
} 

...它從來沒有說

我知道TTS設置正確,因爲它的工作原理在常規Activity

這是我的代碼。

import java.util.HashMap; 
import java.util.Locale; 

import android.app.Service; 
import android.content.Intent; 
import android.media.AudioManager; 
import android.os.IBinder; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; 
import android.util.Log; 

public class TTSService extends Service implements OnInitListener, OnUtteranceCompletedListener { 
TextToSpeech mTTS; 

    @Override 
    public void onCreate() { 
     Log.d("", "TTSService Created!"); 
     mTTS = new TextToSpeech(getApplicationContext(), this); 


     //I've tried it in a thread.... 
     /*new Thread(new Runnable() { 
      @Override 
      public void run() { 
       HashMap<String, String> myHashStream = new HashMap<String, String>(); 
       myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION)); 
       myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1"); 

       mTTS.setLanguage(Locale.US); 
       //mTTS.setOnUtteranceCompletedListener(this); 
       mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);   
      } 

     }).start();*/ 

     //I've tried it not in a thread... 
     HashMap<String, String> myHashStream = new HashMap<String, String>(); 
     myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION)); 
     myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1"); 

     mTTS.setLanguage(Locale.US); 
     mTTS.setOnUtteranceCompletedListener(this); 
     mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);  

    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 

    @Override 
    public void onInit(int status) { 
     Log.d("", "TTSService onInit: " + String.valueOf(status)); 
     if(status == TextToSpeech.SUCCESS){ 
      Log.d("", "TTS Success"); 
     } 
    } 

    public void onUtteranceCompleted(String uttId) { 
     Log.d("", "done uttering"); 
     if(uttId == "1") { 
      mTTS.shutdown(); 
     } 

    } 

} 

感謝

+0

我沒有得到這個工作。我現在將服務中的信息傳遞給我的活動,如果它處於活動狀態。 – Ifor

+1

...是的。我沒有可以傳遞給它的活動。 – bwoogie

回答

3

好吧,我知道了,現在想通了!在TTS初始化之前,它正在嘗試說話。所以在一個線程中,我等待準備好不要== 999.一旦它或者1或者其他任何東西,我們都會照顧說話。這可能並不安全,但將其置於一段時間,但它仍在工作。

import java.util.HashMap; 
import java.util.Locale; 

import android.app.Service; 
import android.content.Intent; 
import android.media.AudioManager; 
import android.os.IBinder; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; 
import android.util.Log; 

public class TTSService extends Service implements OnInitListener, OnUtteranceCompletedListener { 
TextToSpeech mTTS; 
int ready = 999; 
    @Override 
    public void onCreate() { 

     Log.d("", "TTSService Created!"); 
     mTTS = new TextToSpeech(getApplicationContext(), this); 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       while(ready == 999) { 
        //wait 
       } 
       if(ready==1){ 
       HashMap<String, String> myHashStream = new HashMap<String, String>(); 
       myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION)); 
       myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1"); 

       mTTS.setLanguage(Locale.US); 
       //mTTS.setOnUtteranceCompletedListener(this); 
       mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream); 

       } else { 
        Log.d("", "not ready"); 
        } 
      } 

     }).start(); 


     stopSelf(); 

    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 

    @Override 
    public void onDestroy() { 
     mTTS.shutdown(); 
     super.onDestroy(); 
    } 
    @Override 
    public void onInit(int status) { 
     Log.d("", "TTSService onInit: " + String.valueOf(status)); 
     if (status == TextToSpeech.SUCCESS) 
     { 
      ready = 1; 

     } else { 
      ready = 0; 
      Log.d("", "failed to initialize"); 
     } 

    } 

    public void onUtteranceCompleted(String uttId) { 
     Log.d("", "done uttering"); 
     if(uttId == "1") { 
      mTTS.shutdown(); 
     } 

    } 


} 
2

我剛剛也有類似的問題 - 它以mTTS.speak是由於TTS被onInit()開始前被調用。我解決它略有不同放置mTTS.speak()在一個單獨的函數,然後調用該函數從onInit if (TextToSpeech.Successful)內,因此打講話它發起並後不前(從onStart() ECT)。