2

An Android book I have states that using TextToSpeech.playEarcon() is preferable to playing audio files (using MediaPlayer) because:

Instead of having to determine the opportune moment to play an audible cue and relying on callbacks to get the timing right, we can instead queue up our earcons among the text we send to the TTS engine. We then know that our earcons will be played at the appropriate time, and we can use the same pathway to get our sounds to the user, including the onUtteranceCompleted() callbacks to let us know where we are.

But my short and simple experiment with this shows this isn't the case:

String utteranceId = String.valueOf(utteranceNum++);
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
tts.speak("FIRST part of sentence", TextToSpeech.QUEUE_ADD, params);

utteranceId = String.valueOf(utteranceNum++);
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
tts.playEarcon("[fancyring]", TextToSpeech.QUEUE_ADD, params);

utteranceId = String.valueOf(utteranceNum++);
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
tts.speak("SECOND part of sentence", TextToSpeech.QUEUE_ADD, params);

When I examine the logs from onUtteranceCompleted() I only see the utteranceIds of the ones played by tts.speak(), not the one played by tts.playEarcon().

Why is this discrepancy? Is there a workaround for this?

P.S. At the risk of stating the obvious: All three utterances are played out fine and at the right order. It is only the onUtteranceCompleted() that isn't called for some reason for the tts.playEarcon().

4

1 に答える 1

1

自分に答える。TextToSpeech.OnUtteranceCompletedListenerに関する信じられないほど長くて非常に詳細なドキュメントは次のようになっています(強調は私のものです)。

発話が合成されたときに呼び出されます。

イアコンは合成の結果ではないので、もちろんonUtteranceCompleted()が呼び出されることはありません。これは仕様によるものです。

これは私たちを新しい質問に戻します:.mp3ファイルを再生する(MediaPlayerを使用する)よりもイアコンに利点がない場合、なぜイアコンを使用するのですか?

于 2012-04-13T16:48:10.697 に答える