在我的安卓应用中,我使用了文本转语音引擎,让一段文本可以被朗读出来。当手机响铃时,语音代码会运行。但是,日志中出现了以下警告,导致语音断断续续。
请注意,这个问题只在 SDK 15(冰淇淋三明治)上出现,特别是当语音代码在电话响铃时被调用时,否则即使在 ICE 上,语音代码在任何其他时间调用时都能完美运行。该代码在 SDK 8 和 10 上可以正常工作。
我收到的警告如下:
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
相关的代码片段在Service中的一个线程中运行,当接到电话时,该Service会在广播接收器中启动。
// 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);
}
}