3

在我的 android 应用程序中,我使用文本到语音引擎来说出一段文本。语音代码在电话响铃时运行。语音中断,我在 logcat 中看到以下警告。请注意,在 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 的线程中运行,服务在接到电话时在广播接收器中启动。

// 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);           
    }   

}
4

1 回答 1

2

这只是一个猜测,因为这里没有什么可做的,但是如果电话音量在响铃(不振动)到来电时铃声将播放的位置以及某种其他音频时,我已经看到了类似的情况播放(即:TTS/音乐/视频)其中一个音频流变得断断续续/中断,因为它试图同时播放两者。如果这是您希望同时播放两者的效果,那么您可以做的一项工作是PhoneStateListener每隔一段时间延长并暂停音频,然后播放 TTS

PhoneStateListener可以在这里找到扩展的示例

于 2012-06-19T14:40:45.057 回答