2015-10-26 33 views
0

我正在將TextToSpeech合併到GCM gcmquickstart示例中。 收到來自谷歌服務器的消息後,我嘗試用它說話。tts在說話前總是去ondestroy。 但如果使用調試模式,tts可以正常工作。 的代碼如下:TextToSpeech與GCM不起作用

public class MyGcmListenerService extends GcmListenerService implements TextToSpeech.OnInitListener { 

private static final String TAG = "MyGcmListenerService"; 
private TextToSpeech tts; 
private String ttsMessage; 

public void onInit(int code) { 
    if (code == TextToSpeech.SUCCESS) { 
     if (tts != null) { 

      if (Charset.forName("US-ASCII").newEncoder().canEncode(ttsMessage)) { 
       tts.setLanguage(Locale.ENGLISH); 
      } else { 
       tts.setLanguage(Locale.CHINESE); 
      } 

      tts.speak(ttsMessage, TextToSpeech.QUEUE_FLUSH, null); 
      Log.d(TAG, "tts speak!!!"); 
     } 
     Log.d(TAG, "tts initialized!!!"); 
    } else { 
     tts = null; 
    } 
} 

public void onDestroy() { 
    //Close the Text to Speech Library 
    if(tts != null) { 

     tts.stop(); 
     tts.shutdown(); 
     Log.d(TAG, "TTS Destroyed"); 
    } 
    super.onDestroy(); 
} 

public void onMessageReceived(String from, Bundle data) { 
    ttsMessage = message; 
    if (tts == null) { 
     tts = new TextToSpeech(this, this); 
    } 
} 
// [END receive_message] 

} 

回答

0

我找到解決辦法,不是最好的,但它的作品。

在onMessageReceived中運行一個無限循環,這使得tts不被銷燬。 當tts完成工作時,跳出循環。

public class MyGcmListenerService extends GcmListenerService implements TextToSpeech.OnInitListener { 

    private static final String TAG = "MyGcmListenerService"; 
    private TextToSpeech tts; 
    private String ttsMessage; 
    private boolean keepAlive = true; 

    @Override 
    public void onInit(int code) { 
     if (code == TextToSpeech.SUCCESS) { 
      if (tts != null) { 

       if (Charset.forName("US-ASCII").newEncoder().canEncode(ttsMessage)) { 
        tts.setLanguage(Locale.ENGLISH); 
       } else { 
        tts.setLanguage(Locale.CHINESE); 
       } 


       tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { 
        @Override 
        public void onStart(String utteranceId) { 
        } 

        @Override 
        public void onDone(String utteranceId) { 
         keepAlive = false; 
        } 

        @Override 
        public void onError(String utteranceId) { 
        } 
       }); 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID"); 
       tts.speak(ttsMessage, TextToSpeech.QUEUE_FLUSH, map); 
      } 

      Log.d(TAG, "tts speak!!!"); 
     } else { 
      tts = null; 
      keepAlive = false; 
     } 
    } 

    public void onDestroy() { 
    //Close the Text to Speech Library 
    if(tts != null) { 

     tts.stop(); 
     tts.shutdown(); 
     Log.d(TAG, "TTS Destroyed"); 
    } 
    super.onDestroy(); 
    } 

    public void onMessageReceived(String from, Bundle data) { 
    ttsMessage = message; 
    if (tts == null) { 
     tts = new TextToSpeech(this, this); 
    } 
     keepAlive = true; 

     while (keepAlive) { 
      Log.d(TAG, "looping"); 
      ; 
     } 
    } 
    }