2012-08-17 139 views
0

我想使用TextToSpeech,但我有一些奇怪的問題。TTS收到但沒有語音出來

讓我簡單地解釋我的應用程序的體系結構。此應用程序將用於公交車時間。用戶可以將他們的巴士添加到通知。如果有任何更改,服務器將通知訂戶。我想在這個應用程序中使用TTS。

我實現了Android GCM和通知系統,唯一的問題是TTS。

GCMIntentService.onMessage方法調用下面generateNotification方法:

public static void generateNotification(Context context, String message) { 
     int icon = R.drawable.icon_small; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 
     String title = context.getString(R.string.app_name);  

     Bundle b = new Bundle(); 
     b.putString(EXTRA_MESSAGE, message); 
     b.putString(READABLE_MESSAGE, message); 

     Intent notificationIntent = new Intent(context, BusAlerts.class); 
     notificationIntent.putExtra("CallType", CallType.NOTIFICATION); 
     notificationIntent.putExtra("MessageReceived", b); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_NEW_TASK); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND; 
     long[] vibrate = { 1000 }; 
     notification.vibrate = vibrate;   
     notificationManager.notify(0, notification); 
    } 

此代碼成功地產生通知。這裏是BusAlerts.codes(一些微調;))

public class BusAlerts extends Activity implements OnInitListener { 
private static TextToSpeech myTts; 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     myTts = new TextToSpeech(this, null); 
} 

@Override 
protected void onStart() { 
     super.onStart(); 

//readableMessage extracts from bundles 

speak(readableMessage); 
} 

public void speak(String text) 
    { 
     myTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);  
    } 
@Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     if (myTts != null) { 
      myTts.shutdown(); 
      myTts.stop(); 
      myTts = null; 
      } 
    } 
public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 

       int result = myTts.setLanguage(Locale.UK); 

       if (result == TextToSpeech.LANG_MISSING_DATA 
         || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
        Log.e("TTS", "This Language is not supported"); 
       } 
       else{ 
        if (myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE || myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_COUNTRY_AVAILABLE) 
         myTts.setLanguage(Locale.UK); 
       } 

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

    } 

如果用戶打開從通知這個類,一切都運行完美,但沒有聲音出來。當我調試它時,我看到下面的情況。 enter image description here

但是,如果用戶按Home按鈕並重新打開應用程序(同樣的活動恢復),用戶可以聽到聲音。 它的情況就是這樣。 enter image description here

正如我注意到mCachedParamters和mITts不同;但我不知道爲什麼。 我被困住了一個星期,找不到解決方案。

回答

0

你應該叫

speak(readableMessage); 

的OnInit被稱爲之後。 通過在Onstart中調用此函數,您不能保證這一點。這就是當你回到應用程序初始化並且你能聽到的原因

+0

我如何保證它?也正如你所看到的,我不會調用onInit方法。因爲我不會將此參數傳遞給TextToSpeech構造函數。 – 2012-08-17 13:31:21

+0

現在我將onStart中的代碼移動到onCreate,並且我沒有聽到任何通知打開或返回。 – 2012-08-17 13:34:03

+0

你不是在調用它,而是框架調用它。所以你叫說(可讀的消息);在Oninit成功案例中。這個問題僅在oncreate完成時調用。如果你希望在你的應用程序到達前臺時調用它,請移動myTts = new TextToSpeech(this,null); toOnResume – nandeesh 2012-08-17 13:37:57