在我的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);
}
}
很難猜到,請發佈相關代碼示例。 – Sam
@Sam我已更新帖子並添加了相關代碼。 – Ahmed