Real-Time Text To Speech (TTS)

Last updated: 2025-02-06 15:34:46

Integration Preparation

SDK Acquisition

TTS HarmonyOS NEXT SDK and Demo download address: Access SDK Download.

Connection Guidelines

Developers need to view the Real-Time TTS APIs to understand the use requirements and directions before calling them.
The API requires the phone to have an internet connection over 3G, 4G, 5G, Wi-Fi, etc.
To run the demo, you must set the AppID, SecretID, and SecretKey, which can be obtained from API Token Management.

SDK Importing

1. Download and extract the HarmonyOS NEXT SDK compressed package, which includes demo, sdk, and doc. The sdk/realtts_v1.x.x.har is the SDK for real-time TTS.
2. Add dependent libraries in the project by adding the following dependencies in oh-package.jsons.
"dependencies": {
"realtts": "file:../../sdk/realtts_v1.0.0_96b6073.har"
}
Replace the configuration according to the actual path.

Quick Connection

Below is the code snippet from the demo. For the complete code, please refer to the sdk/demo project.

Configuring a Composition Task

let builder = new QCloud.RealTTS.Builder()
builder.appID = this._app_id
builder.secretID = this._secret_id
builder.secretKey = this._secret_key
builder.token = this._token
builder.setApiParam(QCloud.RealTTS.kText, this._text)
builder.setApiParam(QCloud.RealTTS.kVoiceType, this._voice_type)
builder.setApiParam(QCloud.RealTTS.kVolume, this._volume)
builder.setApiParam(QCloud.RealTTS.kSpeed, this._speed)
builder.setApiParam(QCloud.RealTTS.kCodec, 'pcm')
Configure parameters related to the composition task

Starting a Composition Task

Use the above Builder to create and launch the composition task and wait for the result
this._ctl = builder.build({})
await this._ctl.task

Main Interface Class and Method Description

Builder Class Description

Builder is used to create composition tasks
Method

1. setApiParam(key: string, value: string | number | null)

Set parameters for backend requests, refer to the parameters supported by the backend in Real-Time Text to Speech APIs.
Name
Type
Description
key
string
Parameter name for backend request
value
string|number|null
Parameter values for backend requests, null will delete the set parameters, number will be converted to string and then set.
2. getApiParam(key: string): string | number | boolean | null
Get the set request parameters
Name
Type
Description
key
string
Parameter Name
3. build(): Controller
Create a composition task and return the composition task controller
Name
Type
Description
listener
Listener
Data and event callbacks during the streaming synthesis process
Attribute
Name
Type
Description
appID
string
Tencent Cloud appId
secretID
string
Tencent Cloud secretId
secretKey
string
Tencent Cloud secretKey
token
string
Tencent Cloud temporary token, an empty string means no temporary authorization is used. For temporary authorization, refer to Get Federated Identity Temporary Access Credentials.

Controller Class Description

Controller is used to control the process of composition tasks
Method
1. cancel(): void
Cancel the composition task. After success, the task throws a cancellation exception.
Attribute
Name
Type
Description
task
Promise<void>
Composition task

Listener Class Description

Listener data and event callbacks during the streaming synthesis process
1. onLog?: (value: string, level: number) => void
Log callback
Name
Type
Description
value
string
Log message
level
number
Log Level
2. onData?: (data: ArrayBuffer) => void
The audio data returned by the Server-side can be referenced in the document Streaming Text to Speech description.
Name
Type
Description
data
ArrayBuffer
The audio data returned by the Server-side
3. onMessage?: (msg: string) => void
The JSON data returned by the Server-side can be referenced in the document Streaming Text to Speech https://cloud.tencent.com/document/product/1073/108595 description.
Name
Type
Description
msg
ArrayBuffer
The JSON data returned by the Server-side

Error Codes

Error code when the composition task throws an SDKError
Error Code
Name
Description
-1
UNKNOWN_ERROR
Unknown error
1
REQUEST_ERROR
Request failure, please check message and inner_error for details
2
SERVER_ERROR
Server-side returned an error, see message for details
3
SIGNATURE_ERROR
Signature error
4
PARAMETER_ERROR
Parameter error, SDK local parameter validation
5
CANCEL_ERROR
Call cancel succeeded