Added in API level 14

SynthesisCallback


interface SynthesisCallback
android.speech.tts.SynthesisCallback

A callback to return speech data synthesized by a text to speech engine. The engine can provide streaming audio by calling start, then audioAvailable until all audio has been provided, then finally done. #error can be called at any stage in the synthesis process to indicate that an error has occurred, but if the call is made after a call to done, it might be discarded. done must be called at the end of synthesis, regardless of errors. All methods can be only called on the synthesis thread.

Summary

Public methods
abstract Int
audioAvailable(buffer: ByteArray!, offset: Int, length: Int)

The service should call this method when synthesized audio is ready for consumption.

abstract Int

The service should call this method when all the synthesized audio for a request has been passed to audioAvailable.

abstract Unit

The service should call this method if the speech synthesis fails.

abstract Unit
error(errorCode: Int)

The service should call this method if the speech synthesis fails.

abstract Int

abstract Boolean

Check if done was called or not.

abstract Boolean

Check if start was called or not.

open Unit
rangeStart(markerInFrames: Int, start: Int, end: Int)

The service may call this method to provide timing information about the spoken text.

abstract Int
start(sampleRateInHz: Int, audioFormat: Int, channelCount: Int)

The service should call this when it starts to synthesize audio for this request.

Public methods

audioAvailable

Added in API level 14
abstract fun audioAvailable(
    buffer: ByteArray!,
    offset: Int,
    length: Int
): Int

The service should call this method when synthesized audio is ready for consumption.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Parameters
buffer ByteArray!: The generated audio data. This method will not hold on to buffer, so the caller is free to modify it after this method returns.
offset Int: The offset into buffer where the audio data starts.
length Int: The number of bytes of audio data in buffer. This must be less than or equal to the return value of getMaxBufferSize.
Return
Int android.speech.tts.TextToSpeech#SUCCESS, android.speech.tts.TextToSpeech#ERROR or android.speech.tts.TextToSpeech#STOPPED.

done

Added in API level 14
abstract fun done(): Int

The service should call this method when all the synthesized audio for a request has been passed to audioAvailable.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

This method has to be called if start and/or #error was called.

Return
Int android.speech.tts.TextToSpeech#SUCCESS, android.speech.tts.TextToSpeech#ERROR or android.speech.tts.TextToSpeech#STOPPED.

error

Added in API level 14
abstract fun error(): Unit

The service should call this method if the speech synthesis fails.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

error

Added in API level 21
abstract fun error(errorCode: Int): Unit

The service should call this method if the speech synthesis fails.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Parameters
errorCode Int: Error code to pass to the client. One of the ERROR_ values from Value is android.speech.tts.TextToSpeech#ERROR_SYNTHESIS, android.speech.tts.TextToSpeech#ERROR_SERVICE, android.speech.tts.TextToSpeech#ERROR_OUTPUT, android.speech.tts.TextToSpeech#ERROR_NETWORK, android.speech.tts.TextToSpeech#ERROR_NETWORK_TIMEOUT, android.speech.tts.TextToSpeech#ERROR_INVALID_REQUEST, or android.speech.tts.TextToSpeech#ERROR_NOT_INSTALLED_YET

getMaxBufferSize

Added in API level 14
abstract fun getMaxBufferSize(): Int
Return
Int the maximum number of bytes that the TTS engine can pass in a single call of audioAvailable. Calls to audioAvailable with data lengths larger than this value will not succeed.

hasFinished

Added in API level 21
abstract fun hasFinished(): Boolean

Check if done was called or not.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Useful for checking if a fallback from network request is possible.

hasStarted

Added in API level 21
abstract fun hasStarted(): Boolean

Check if start was called or not.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Useful for checking if a fallback from network request is possible.

rangeStart

Added in API level 26
open fun rangeStart(
    markerInFrames: Int,
    start: Int,
    end: Int
): Unit

The service may call this method to provide timing information about the spoken text.

Calling this method means that at the given audio frame, the given range of the input is about to be spoken. If this method is called the client will receive a callback on the listener (UtteranceProgressListener#onRangeStart) at the moment that frame has been reached by the playback head.

This information can be used by the client, for example, to highlight ranges of the text while it is spoken.

The markerInFrames is a frame index into the audio for this synthesis request, i.e. into the concatenation of the audio bytes sent to audioAvailable for this synthesis request. The definition of a frame depends on the format given by start. See AudioFormat for more information.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Parameters
markerInFrames Int: The position in frames in the audio where this range is spoken.
start Int: The start index of the range in the input text.
end Int: The end index (exclusive) of the range in the input text.

start

Added in API level 14
abstract fun start(
    sampleRateInHz: Int,
    audioFormat: Int,
    channelCount: Int
): Int

The service should call this when it starts to synthesize audio for this request.

This method should only be called on the synthesis thread, while in android.speech.tts.TextToSpeechService#onSynthesizeText.

Parameters
sampleRateInHz Int: Sample rate in HZ of the generated audio.
audioFormat Int: Audio format of the generated audio. Must be one of android.media.AudioFormat#ENCODING_PCM_8BIT or AudioFormat#ENCODING_PCM_16BIT. Can also be AudioFormat#ENCODING_PCM_FLOAT when targetting Android N and above. Value is android.media.AudioFormat#ENCODING_PCM_8BIT, android.media.AudioFormat#ENCODING_PCM_16BIT, or android.media.AudioFormat#ENCODING_PCM_FLOAT
channelCount Int: The number of channels. Must be 1 or 2. Value is between 1 and 2 inclusive
Return
Int android.speech.tts.TextToSpeech#SUCCESS, android.speech.tts.TextToSpeech#ERROR or android.speech.tts.TextToSpeech#STOPPED.