public class TtsClient extends Object
The methods include speakText(java.lang.String,java.lang.String),
speakLongText(java.lang.String,java.lang.String), and stopSpeak().
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
METHOD_CREATE |
Identifies the
create(ohos.app.Context,ohos.ai.tts.TtsListener) method. |
static int |
METHOD_DESTROY |
Identifies the
destroy() method. |
static int |
METHOD_RELEASE |
Identifies the
release() method. |
static int |
METHOD_SPEAK_LONG_TEXT |
Identifies the
speakLongText(java.lang.String,java.lang.String) method. |
static int |
METHOD_SPEAK_TEXT |
Identifies the
speakText(java.lang.String,java.lang.String) method. |
static int |
METHOD_STOP_SPEAK |
Identifies the
stopSpeak() method. |
static String |
PAC_MAP_KEY_METHOD_ID |
Indicates the key of the input parameter
PacMap for the TtsListener#onEvent(int, PacMap) method. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
create(Context context,
TtsListener ttsListener) |
Creates a TTS client.
|
void |
destroy() |
Destroys the TTS client.
|
static TtsClient |
getInstance() |
Returns a singleton instance of
TtsClient. |
Optional<String> |
getVersion() |
Obtains the version of TTS software development kit (SDK).
|
boolean |
init(TtsParams ttsParams) |
Initializes the TTS client.
|
boolean |
isSpeaking() |
Checks whether TTS speaking is in progress.
|
void |
release() |
Releases the TTS engine in use.
|
boolean |
setAudioType(AudioManager.AudioVolumeType audioType) |
Sets the audio stream type.
|
boolean |
setParams(TtsParams ttsParams) |
Sets TTS parameters.
|
void |
speakLongText(String longText,
String utteranceId) |
Synthesizes long text into audio and starts speaking.
|
void |
speakText(String text,
String utteranceId) |
Synthesizes text into audio and starts speaking.
|
void |
stopSpeak() |
Stops TTS speaking.
|
public static final int METHOD_CREATE
create(ohos.app.Context,ohos.ai.tts.TtsListener) method.public static final int METHOD_DESTROY
destroy() method.public static final int METHOD_RELEASE
release() method.public static final int METHOD_SPEAK_LONG_TEXT
speakLongText(java.lang.String,java.lang.String) method.public static final int METHOD_SPEAK_TEXT
speakText(java.lang.String,java.lang.String) method.public static final int METHOD_STOP_SPEAK
stopSpeak() method.public static final String PAC_MAP_KEY_METHOD_ID
PacMap for the TtsListener#onEvent(int, PacMap) method.public static TtsClient getInstance()
TtsClient.
If there is a TtsClient object, this method will return it to the caller. If there is no
TtsClient object, this method will create one and return it to the caller.
TtsClient.public void create(Context context, TtsListener ttsListener)
You can obtain the creation result (success or failure) from TtsListener#onEvent(int, PacMap).
context - Indicates the application context. This parameter cannot be null.ttsListener - Indicates the listener for observing the TTS callback process and result. You are not advised
to set this parameter to null because the null value will cause a failure to obtain the execution status
of methods in this class.public void release()
Calling this method will set the initialization status to false and stop the ongoing speaking task.
Therefore, you are advised to use this method before calling init(ohos.ai.tts.TtsParams).
public boolean init(TtsParams ttsParams)
You can set the device ID, device type, speaking speed, volume, pitch, and timbre in the input parameter
ttsParams. Among them, the device ID is mandatory. If it is not set, the initialization will fail.
ttsParams - Indicates initialization parameters, including the following fields:
0 represents a mobile phone,1 for a speaker, 2 for a web-based device,3
for a tablet, and 4 for a large screen.
5 as the default value11 as the
default value5 as the default value0 for SPEAKER_XIAOYI and 1 for SPEAKER_YOYO, with 0 as the default valueIf the value for any of these fields is incorrect, the TTS engine will fail to be initialized.
true if the initialization is successful; returns false otherwise.public boolean setParams(TtsParams ttsParams)
You can use this method to modify the speaking volume, pitch, speed, and timbre in ttsParams. You are
advised to use this method after you have called init(ohos.ai.tts.TtsParams).
ttsParams - Indicates TTS parameters (speaking volume, pitch, speed, and timbre, each ranging from 0 to 15).
If any of these parameters is invalid, the parameter settings will fail; if other parameters (device ID
and device type) are passed to this method without verification, the parameters will not be used even if
the settings are successful. Instead, the TTS engine will use the parameters (device ID and device type)
in the init(ohos.ai.tts.TtsParams) method.
true if the setting is successful; returns false otherwise.public boolean setAudioType(AudioManager.AudioVolumeType audioType)
You can call this method before enabling audio speaking.
audioType - Indicates the audio stream type. Currently, the following types are supported:
AudioManager.AudioVolumeType.STREAM_MUSIC (default value)AudioManager.AudioVolumeType.STREAM_VOICE_CALLAudioManager.AudioVolumeType.STREAM_SYSTEMAudioManager.AudioVolumeType.STREAM_RINGAudioManager.AudioVolumeType.STREAM_NOTIFICATIONAudioManager.AudioVolumeType.STREAM_ALARMAudioManager.AudioVolumeType.STREAM_DTMFAudioManager.AudioVolumeType.STREAM_TTStrue if the setting is successful; returns false otherwise.public void speakText(String text, String utteranceId)
text - Indicates the text to speak. The maximum text length is 512. If the length exceeds 512 or the text is
empty, an error occurs and is passed in TtsListener#onError(String, String) to the caller.utteranceId - Uniquely identifies a speaking request. If this parameter is null or an empty string, the TTS
engine randomly generates a value for this parameter.public void speakLongText(String longText, String utteranceId)
This method is an enhancement of speakText(java.lang.String,java.lang.String).
longText - Indicates the long text to speak. The maximum length for long text is 100000. If the length
exceeds 100000 or the text is empty, an error occurs and is passed in
TtsListener#onError(String, String) to the caller.utteranceId - Uniquely identifies a speaking request. If this parameter is null or an empty string, the TTS
engine randomly generates a value for this parameter.public boolean isSpeaking()
true if TTS speaking is in progress; returns false otherwise.public void stopSpeak()
If a TTS task stopped by this method is restarted, speaking starts from the beginning instead of where the previous speaking stopped.
public Optional<String> getVersion()
Optional. You can call the get method in
Optional to obtain the SDK version number represented by a string.Optionalpublic void destroy()
You must use this method after all ongoing TTS tasks are complete.