Implements the single TTS task playback callback, with which the TTS playback status and error and alarm events are listened to.
| Return type | Method name |
| void | onAudioAvailable(String taskId, MLTtsAudioFragment audioFragment, int offset, Pair<Integer, Integer> range, Bundle bundle) |
| void | onError(String taskId, MLTtsError err) |
| void | onEvent(String taskId, int eventId, android.os.Bundle bundle) |
| void | onRangeStart(String taskId, int start, int end) |
| void | onWarn(String taskId, MLTtsWarn warn) |
Parameters
| Parameter name | Parameter desc |
| taskId | ID of an audio synthesis task corresponding to an audio. |
| audioFragment | Audio data. |
| offset | Offset of the audio segment to be transmitted in the queue. One audio synthesis task corresponds to an audio synthesis queue. |
| range | Text area where the audio segment to be transmitted is located. range.first (included): start position; range.second (excluded): end position. The unit is character. A single Chinese character is counted as one character. For example, text "Hello, ML!" is input for an audio synthesis task and will be split into "Hello!" and "ML" parts. The two parts correspond to ranges (0, 5) and (5, 11), respectively. |
| bundle | Extended parameter. |
| Method |
| public void onError(String taskId, MLTtsError err) |
| When an error occurs in a TTS task, this API can be used to listen to error events. |
Parameters
| Parameter name | Parameter desc |
| taskId | ID of a TTS task where an error occurs. |
| err | Error details. |
Parameters
| Parameter name | Parameter desc |
| taskId | ID of a TTS task corresponding to a callback event. |
| eventId | Callback event ID.
MLTtsConstants.EVENT_PLAYSTART: called when playback starts. MLTtsConstants.EVENT_PLAYRESUME: called when playback resumes. MLTtsConstants.EVENT_PLAYPAUSE: called when playback pauses. MLTtsConstants.EVENT_PLAYSTOP: called when playback stops. This API carries the parameter interrupted of the boolean type. true: The playback ends abnormally and is interrupted by an external error, for example, the QUEUE_FLUSH mode is used, the stop API is called, or an exception occurs. false: The playback ends normally.
MLTtsConstants.EVENT_SYNTHESIS_START: called when audio synthesis starts. MLTtsConstants.EVENT_SYNTHESIS_END: called when audio synthesis ends. MLTtsConstants.EVENT_SYNTHESIS_COMPLETE: called when TTS event processing completes. This API is called when OPEN_STREAM is enabled (refer to speak) and all streams are transmitted. This API carries the parameter interrupted of the boolean type. true: The audio synthesis task ends abnormally and is interrupted by an external error, for example, the QUEUE_FLUSH mode is used, the stop API is called, or an exception occurs. false: The audio synthesis task ends normally. |
| bundle | Extended parameter, which is used together with eventId.
|
Sample Code
- switch (eventId){
- // Pay attention to the following callback APIs when you use the internal player to play the synthesized audio:
- case MLTtsConstants.EVENT_PLAY_START:
- // Called when playback starts.
- break;
- case MLTtsConstants.EVENT_PLAY_STOP:
- // Called when playback stops.
- boolean isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_PLAYSTOP_INTERRUPTED);
- break;
- case MLTtsConstants.EVENT_PLAY_PAUSE:
- // Called when playback pauses.
- break;
- case MLTtsConstants.EVENT_PLAY_RESUME:
- // Called when playback resumes.
- break;
- // Pay attention to the following callback APIs when you focus on only the synthesized audio data but do not use the internal player for playback:
- case MLTtsConstants.EVENT_SYNTHESIS_START:
- // Called when audio synthesis starts.
- break;
- case MLTtsConstants.EVENT_SYNTHESIS_END:
- // Called when audio synthesis ends, and the audio streams may be still being transmitted by calling onAudioAvailable.
- break;
- case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
- // Called after all audio streams are transmitted to the player in default internal playback scenarios.
- // Called when all audio streams are transmitted to the app in OPEN_STREAM and EXTERNAL_PLAYBACK modes.
- boolean isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED);
- break;
- default:
- break;
- }
Parameters
| Parameter name | Parameter desc |
| taskId | ID of a TTS task. |
| start | Start position of an audio segment in the input text. |
| end | End position (excluded) in the input text of the current audio synthesis task corresponding to the audio segment. |
Sample Code
- // The speak API passes the text "Hello Tom!How are you?"
- MLTtsEngine ttsEngine = new MLTtsEngine(null);
- ttsEngine.setTtsCallback(new new MLTtsCallback() {
- ...
- public void onRangeStart(String taskId, int start, int end) {
- // The SDK calls the APIs as follows:
- // onRangeStart("1587653499796229", 0, 10); indicates "Hello Tom!" starts to be played.
- // onRangeStart("1587653499796229", 10, 22); indicates "How are you?" starts to be played.
- }
- ...
- });
- ttsEngine.speak("Hello world!"); // The returned taskId is 1587653499796229.
| Method |
| public void onWarn(String taskId, MLTtsWarn warn) |
| Implements the callback for alarm events. This API is used only for alarm notifications. Your app can determine whether to ignore the notifications based on service requirements. For example, when the network status is poor, or the bandwidth is insufficient, your app will report WARN_INSUFFICIENT_BANDWIDTH. In this way, you can determine whether to notify the user or ignore the notification. |
Parameters
| Parameter name | Parameter desc |
| taskId | ID of a TTS task where an alarm is reported. |
| warn | Alarm details. |
Sample Code
- warnId = MLTtsWarn.WARN_INSUFFICIENT_BANDWIDTH.