Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.
1. Create a TTS engine. You can create a TTS engine using MLTtsConfig.
- // Use customized parameter settings to create a TTS engine.
- MLTtsConfig mlConfigs = new MLTtsConfig()
- // Set the text converted from speech to Chinese.
- // MLTtsConstants.TTS_EN_US: converts text to English.
- // MLTtsConstants.TTS_ZH_HANS: converts text to Chinese.
- // MLTtsConstants.TTS_LAN_ES_ES: converts text to Spanish.
- // MLTtsConstants.TTS_LAN_FR_FR: converts text to French.
- .setLanguage(MLTtsConstants.TTS_ZH_HANS)
- // Set the Chinese timbre.
- // MLTtsConstants.TTS_SPEAKER_FEMALE_ZH: Chinese female voice.
- // MLTtsConstants.TTS_SPEAKER_MALE_ZH: Chinese male voice.
- // Set the English timbre.
- // MLTtsConstants.TTS_SPEAKER_FEMALE_EN: English female voice.
- // MLTtsconstants.TTS_SPEAKER_MALE_EN: English male voice.
- // MLTtsConstants.TTS_SPEAKER_FEMALE_FR: French female voice.
- // MLTtsconstants.TTS_SPEAKER_FEMALE_ES: Spanish female voice.
- .setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)
- // Set the speech speed. Range: 0–2. 1.0 indicates normal speed.
- .setSpeed(1.0f)
- // Set the volume. Range: 0–2. 1.0 indicates normal volume.
- .setVolume(1.0f);
- MLTtsEngine mlTtsEngine = new MLTtsEngine(mlConfigs);
- // Update the configuration when the engine is running.
- mlTtsEngine.updateConfig(mlTtsConfig);
2. Create a TTS callback function to process the audio synthesis result.
- MLTtsCallback callback = new MLTtsCallback() {
- @Override
- public void onError(String taskId, MLTtsError err) {
- // Processing logic for TTS failure.
- }
- @Override
- public void onWarn(String taskId, MLTtsWarn warn) {
- // Alarm handling without affecting service logic.
- }
- @Override
- // Return the mapping between the currently played segment and text. start: start position of the audio segment in the input text; end (excluded): end position of the audio segment in the input text.
- public void onRangeStart(String taskId, int start, int end) {
- // Process the mapping between the currently played segment and text.
- }
- @Override
- // taskId: ID of an audio synthesis task corresponding to the 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.
- public void onAudioAvailable(String taskId, MLTtsAudioFragment audioFragment, int offset, Pair<Integer, Integer> range,
- Bundle bundle){
- // Audio stream callback API, which is used to return the synthesized audio data to the app.
- }
- @Override
- public void onEvent(String taskId, int eventId, Bundle bundle) {
- // Callback method of an audio synthesis event. eventId: event name.
- switch (eventId) {
- 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_RESUME:
- // Called when playback resumes.
- break;
- case MLTtsConstants.EVENT_PLAY_PAUSE:
- // Called when playback pauses.
- break;
- }
- // Pay attention to the following callback events when you focus on only 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.
- break;
- case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
- // Audio synthesis is complete. All synthesized audio streams are passed to the app.
- boolean isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED);
- break;
- default:
- break;
- }
- }
3. Pass the TTS callback to the TTS engine created in step 1 to perform audio synthesis.
- mlTtsEngine.setTtsCallback(callback);
- /**
- *First parameter sourceText: text information to be synthesized. The value can contain a maximum of 500 characters.
- * Second parameter indicating the synthesis mode: The format is configA | configB | configC.
- *configA:
- * MLTtsEngine.QUEUE_APPEND: After an audio synthesis task is generated, the audio synthesis task is processed as follows: If playback is going on, the task is added to the queue for execution in sequence; if playback pauses, the playback is resumed and the task is added to the queue for execution in sequence; if there is no playback, the audio synthesis task is executed immediately.
- * MLTtsEngine.QUEUE_FLUSH: The ongoing audio synthesis task and playback are stopped immediately, all audio synthesis tasks in the queue are cleared, and the current audio synthesis task is executed immediately and played.
- *configB:
- * MLTtsEngine.OPEN_STREAM: The synthesized audio data is output through onAudioAvailable.
- *configC:
- * MLTtsEngine.EXTERNAL_PLAYBACK: external playback mode. The player provided by the SDK is shielded. You need to process the audio output by the onAudioAvailable callback API. In this case, the playback-related APIs in the callback APIs become invalid, and only the callback APIs related to audio synthesis can be listened.
- */
- // Use the built-in player of the SDK to play speech in queuing mode.
- String id = mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND);
- // In queuing mode, the synthesized audio stream is output through onAudioAvailable, and the built-in player of the SDK is used to play the speech.
- // String id = mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND | MLTtsEngine.OPEN_STREAM);
- // In queuing mode, the synthesized audio stream is output through onAudioAvailable, and the audio stream is not played, but controlled by you.
- // String id = mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND | MLTtsEngine.OPEN_STREAM | MLTtsEngine.EXTERNAL_PLAYBACK);
4. Control the playback (valid only when an internal player is used).
- // Pause playback.
- mlTtsEngine.pause();
- // Resume playback.
- mlTtsEngine.resume();
5. Stop the current TTS task, clear all tasks (including tasks that are being played and have not been played) in the queue, and retain the TTS engine.
- mlTtsEngine.stop();
6. Destroy the TTS engine and release related resources.
- if (mlTtsEngine!= null) {
- mlTtsEngine.shutdown();
- }