SynthesisCallback
  public
  
  
  
  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(int, int, int), then audioAvailable(byte, int, int) 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(byte[] buffer, int offset, int length)
      The service should call this method when synthesized audio is ready for consumption. | 
| 
        abstract
        
        
        
        
        int | 
      done()
      The service should call this method when all the synthesized audio for a request has been
 passed to  | 
| 
        abstract
        
        
        
        
        void | 
      error(int errorCode)
      The service should call this method if the speech synthesis fails. | 
| 
        abstract
        
        
        
        
        void | 
      error()
      The service should call this method if the speech synthesis fails. | 
| 
        abstract
        
        
        
        
        int | 
      getMaxBufferSize()
       | 
| 
        abstract
        
        
        
        
        boolean | 
      hasFinished()
      Check if  | 
| 
        abstract
        
        
        
        
        boolean | 
      hasStarted()
      Check if  | 
| 
        
        default
        
        
        
        void | 
      rangeStart(int markerInFrames, int start, int end)
      The service may call this method to provide timing information about the spoken text. | 
| 
        abstract
        
        
        
        
        int | 
      start(int sampleRateInHz, int audioFormat, int channelCount)
      The service should call this when it starts to synthesize audio for this request. | 
Public methods
audioAvailable
public abstract int audioAvailable (byte[] buffer, 
                int offset, 
                int length)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 TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
| Parameters | |
|---|---|
| buffer | byte: The generated audio data. This method will not hold on tobuffer, so the
     caller is free to modify it after this method returns. | 
| offset | int: The offset intobufferwhere the audio data starts. | 
| length | int: The number of bytes of audio data inbuffer. This must be less than or
     equal to the return value ofgetMaxBufferSize(). | 
| Returns | |
|---|---|
| int | TextToSpeech.SUCCESS,TextToSpeech.ERRORorTextToSpeech.STOPPED. | 
done
public abstract int done ()
The service should call this method when all the synthesized audio for a request has been
 passed to audioAvailable(byte, int, int).
 
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
 
This method has to be called if start(int, int, int) and/or error() was called.
| Returns | |
|---|---|
| int | TextToSpeech.SUCCESS,TextToSpeech.ERRORorTextToSpeech.STOPPED. | 
error
public abstract void error (int errorCode)
The service should call this method if the speech synthesis fails.
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
| Parameters | |
|---|---|
| errorCode | int: Error code to pass to the client. One of the ERROR_ values fromTextToSpeechValue isTextToSpeech.ERROR_SYNTHESIS,TextToSpeech.ERROR_SERVICE,TextToSpeech.ERROR_OUTPUT,TextToSpeech.ERROR_NETWORK,TextToSpeech.ERROR_NETWORK_TIMEOUT,TextToSpeech.ERROR_INVALID_REQUEST, orTextToSpeech.ERROR_NOT_INSTALLED_YET | 
error
public abstract void error ()
The service should call this method if the speech synthesis fails.
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
getMaxBufferSize
public abstract int getMaxBufferSize ()
| Returns | |
|---|---|
| int | the maximum number of bytes that the TTS engine can pass in a single call of audioAvailable(byte, int, int). Calls toaudioAvailable(byte, int, int)with data lengths larger than this
     value will not succeed. | 
hasFinished
public abstract boolean hasFinished ()
Check if done() was called or not.
 
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
 
Useful for checking if a fallback from network request is possible.
| Returns | |
|---|---|
| boolean | |
hasStarted
public abstract boolean hasStarted ()
Check if start(int, int, int) was called or not.
 
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
 
Useful for checking if a fallback from network request is possible.
| Returns | |
|---|---|
| boolean | |
rangeStart
public void rangeStart (int markerInFrames, 
                int start, 
                int end)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(int, int, int). See AudioFormat
 for more information.
 
This method should only be called on the synthesis thread, while in TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
| 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
public abstract int start (int sampleRateInHz, 
                int audioFormat, 
                int channelCount)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 TextToSpeechService.onSynthesizeText(SynthesisRequest, SynthesisCallback).
| Parameters | |
|---|---|
| sampleRateInHz | int: Sample rate in HZ of the generated audio. | 
| audioFormat | int: Audio format of the generated audio. Must be one ofAudioFormat.ENCODING_PCM_8BITorAudioFormat.ENCODING_PCM_16BIT. Can also beAudioFormat.ENCODING_PCM_FLOATwhen targetting Android N and above.
 Value isAudioFormat.ENCODING_PCM_8BIT,AudioFormat.ENCODING_PCM_16BIT, orAudioFormat.ENCODING_PCM_FLOAT | 
| channelCount | int: The number of channels. Must be1or2.
 Value is between 1 and 2 inclusive | 
| Returns | |
|---|---|
| int | TextToSpeech.SUCCESS,TextToSpeech.ERRORorTextToSpeech.STOPPED. | 
