2012-06-16 43 views
3

在我的Android應用程序中,我使用了文本到語音引擎來使用一段文字。語音代碼在電話響鈴時運行。演講中斷了,我在logcat中看到以下警告。 請注意,在SDK 15(冰淇淋三明治)上可以看到此問題,特別是只有當電話響鈴時調用語音代碼時,否則即使在ICE上,當電話沒有響鈴時調用其他任何時間時,語音代碼也能正常工作。代碼工作罰款SDK 8和10Android文本到語音在電話鈴響時壞掉

的警告,我得到的是:

com.svox.pico W/AudioTrack(549): obtainBuffer() track 0x177dd8 disabled, restarting 
com.svox.pico W/AudioTrack(549): obtainBuffer() track 0x177dd8 disabled, restarting 
com.svox.pico D/dalvikvm(549): GC_CONCURRENT freed 441K, 8% free 6552K/7111K, paused 3ms+107ms 
com.svox.pico W/AudioTrack(549): obtainBuffer() track 0x156058 disabled, restarting 
com.svox.pico D/dalvikvm(162): GC_CONCURRENT freed 532K, 11% free 8391K/9415K, paused 5ms+6ms 

相關的代碼在一個線程中運行的服務,服務是在廣播接收器,當它啓動打個電話。

// The constructor of speaker class 
public Speaker(final Context context, final Settings settings) 
{ 
    this.settings = settings; 
    params = new HashMap<String, String>();  
    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM)); 
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "myapp"); 

    this.context = context;  

    synchronized (synch) 
    { 
     talker = new TextToSpeech(context, this); 
    } 

    Utils.log(TAG, "Created TextToSpeech.."); 
} 

@Override 
public void onInit(final int status) 
{ 
    synchronized (synch) 
    { 
     Utils.log(TAG, "TTS onInit.."); 

     if(talker == null) 
     { 
      throw new RuntimeException(Utils.collectPlatformInfo(context)); 
     } 

     if(TextToSpeech.ERROR == talker.setOnUtteranceCompletedListener(new SpeechFinishedListener())) 
     { 
      Utils.log(TAG, "Error tts setUt"); 
      return ; 
     } 

     int code = talker.setLanguage(Locale.getDefault()); 

     if(code == TextToSpeech.LANG_NOT_SUPPORTED || code ==TextToSpeech.LANG_MISSING_DATA) 
     { 
      Utils.log(TAG, String.format("Error settingLang on TTS code %d", code));    
     } 

     if(TextToSpeech.ERROR == talker.setSpeechRate(settings.getSpeed())) 
     { 
      //this error is not a fatal, we can continue 
      Utils.log(TAG, "Error tts setSPeechrate");      
     } 

     if(TextToSpeech.ERROR == talker.setPitch(settings.getPitch())) 
     { 
      //this error is not a fatal, we can continue 
      Utils.log(TAG, "Error tts setPitch");   
     } 

     ready = true; 
    } 
} 

    // The function used to provide text to be spoken.. 
public void speak(final String text) throws InterruptedException 
{ 
    if(bEngineFailure) 
     return; 

    if(!ready) 
    { 
     Utils.log(TAG, "engine not initialized"); 
     bEngineFailure = true; 
     return; 
    } 

    bSpeechComplete = false; 

    talker.speak(text, TextToSpeech.QUEUE_ADD, params); 

    while(!bSpeechComplete) 
    { 
      Thread.sleep(100);   
    } 

} 
+0

很難猜到,請發佈相關代碼示例。 – Sam

+0

@Sam我已更新帖子並添加了相關代碼。 – Ahmed

回答

2

這只是一個猜測,因爲沒有太多去這裏的,但我已經看到了類似的情況,如果該手機的體積上環(不震動)的地方,當有來電時鈴聲會玩和某種其他音頻正在播放(即:TTS /音樂/視頻),其中一個音頻流會因爲試圖在同一時間播放而出現斷斷續續的情況。如果這是你想要的效果在同一時間同時播放,然後一個解決辦法,你可以做的是延長PhoneStateListener並每隔一段時間暫停音頻然後打延長PhoneStateListener的TTS

例子可以發現here

+0

嘗試在電話響鈴狀態期間播放語音,直到薑餅和更早版本。我嘗試過無聲鈴聲,但無濟於事。不過,我已經選擇了一些應用程序在ICS中同時播放語音​​和鈴聲,但沒有問題。 – Ahmed

+0

你知道它們是什麼應用程序嗎? – tyczj