Streaming Text To Speech

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

Streaming Text-To-Speech (IOS)

Development Environment

1. iOS 9.0 or above is supported
2. The mobile phone needs to connect to the network
3. Speech synthesis SDK for iOS

Importing SDK

Add QCloudStreamTTS.xcframework from the SDK folder to the project's linked libraries.

API Description

QCloudStreamTTSController

Streaming Text Synthesis Controller
@protocol QCloudStreamTTSController <NSObject>

/*
* Send synthetic text, needs to be called after onReady, otherwise the sent text will not be processed
* @param text Text to be synthesized
*/
-(void)synthesis:(nonnull NSString*)text;
/*
* Cancel the composition task
*/
-(void)cancel;
/*
* Stop the composition task
*/
-(void)stop;

@end

QCloudStreamTTSConfig

Streaming Text Synthesis Configuration
@interface QCloudStreamTTSConfig : NSObject

@property (nonnull) NSString* appID; // Tencent Cloud appid
@property (nonnull) NSString* secretID; // Tencent Cloud secretID
@property (nonnull) NSString* secretKey; // Tencent Cloud secretKey
@property (nonnull) NSString* token; // Temporary token, effective when not an empty string. When using a temporary token, secretId and secretKey must be temporary keys
@property int connectTimeout; // Effective when greater than 0, unit is ms, default is 0

/*
* Set the parameters for the API sent to the backend. For parameter details, please refer to the document https://cloud.tencent.com/document/product/1073/108595
* @param key Parameter name
* @param value Parameter value, if the parameter value is nil, the set key will be deleted
*/
- (QCloudStreamTTSConfig*)setApiParam:(nonnull NSString*)key value:(nullable NSString*)value;
- (QCloudStreamTTSConfig*)setApiParam:(nonnull NSString*)key ivalue:(NSInteger)value;
- (QCloudStreamTTSConfig*)setApiParam:(nonnull NSString*)key fvalue:(float)value;
- (QCloudStreamTTSConfig*)setApiParam:(nonnull NSString*)key bvalue:(BOOL)value;

/*
* Create a streaming TTS controller
* @param listener API for callback of composition task and intermediate information
*/
- (id<QCloudStreamTTSController>)build:(id<QCloudStreamTTSListener>)listener;

@end

QCloudStreamTTSListener

@protocol QCloudStreamTTSListener <NSObject>

@required
/*
* Composition task ended
*/
-(void)onFinish;
/*
* Synthesis task error
* @param error Error message
*/
-(void)onError:(nonnull NSError*)error;

@optional
/*
* Composition log
* @param value Log information
* @param level Log level
*/
-(void)onLog:(nonnull NSString*)value level:(int)level;
/*
* The audio data returned by the server-side. For details, please refer to the document https://cloud.tencent.com/document/product/1073/108595
* @param data The audio data returned by the server-side
*/
-(void)onData:(nonnull NSData*)data;
/*
* The json data returned by the server-side. For details, please refer to the document https://cloud.tencent.com/document/product/1073/108595
* @param msg The json data returned by the server-side
*/
-(void)onMessage:(nonnull NSString*)msg;
/*
* The ready event returned by the server-side
*/
-(void)onReady;

@end

Error Code

The NSError of the composition task is only returned through the onError callback of QCloudStreamTTSListener.
enum : NSInteger {
STREAMTTSPARAMETERERROR = 2000, // Parameter error, there is an issue with the SDK configuration, usually due to missing authorization information
STREAMTTSWEBSOCKETERROR = 2001, // WebSocket error, network issue
STREAMTTSCANCELERROR = 2002, // Cancel error, successfully calling cancel returns this error
STREAMTTSSERVERERROR = 2003, // Server-side returned an error, detailed information can be obtained from the Message in userInfo
};