This topic describes how to use the legacy iOS software development kit (SDK) for Alibaba Cloud Voice Service. It includes installation instructions and code samples. Note that new users should use the latest iOS SDK.
We recommend that you use the latest version of the iOS SDK. This legacy version is no longer updated. For more information, see iOS SDK.
Prerequisites
Read the API reference. For more information, see API reference.
Create a project in the Voice Service console and obtain an appkey. For more information, see Create a project.
Obtain an access token for Voice Service. For more information, see Obtain an access token overview.
Speech synthesis
Speech synthesis converts text to speech. The service supports multiple voices and provides outputs in PCM, WAV, or MP3 format. The example shows how to synthesize and play back audio in PCM format.
Call procedure
Import the SDK into your project as an embedded binary.
Import the AliyunNlsClientAdaptor.h, NlsSpeechSynthesizerRequest.h, and SynthesizerRequestParam.h header files from the NlsSdk.
Implement the NlsSpeechSynthesizerDelegate callback method of NlsSpeechSynthesizerRequest.
Create an AliyunNlsClientAdaptor object named nlsClient.
You only need to create this global object once because it can be reused.
Call the createSynthesizerRequest method of the nlsClient object to obtain an NlsSpeechSynthesizerRequest object.
This NlsSpeechSynthesizerRequest object cannot be reused. Create a new object for each request.
Set parameters, such as the access token and appkey, in the SynthesizerRequestParam object.
Pass the SynthesizerRequestParam object that you set in Step 5 to the setSynthesizerParams method of NlsSpeechSynthesizerRequest.
Call the start method of the NlsSpeechSynthesizerRequest object to start speech synthesis.
The synthesized audio data is passed through the
-(void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length;method of the NlsSpeechSynthesizerDelegate callback.Use the NLSPlayAudio utility to play the audio data received in Step 8.
Key interfaces
AliyunNlsClientAdaptor: The speech processing client. You can use this client for short sentence recognition, real-time speech recognition, and speech synthesis tasks. This client is thread-safe. Create only one global instance.
NlsSpeechSynthesizerRequest: The request object for speech synthesis. This object is thread-safe.
SynthesizerRequestParam: Parameters related to speech synthesis.
NlsSpeechSynthesizerDelegate: The delegate for speech synthesis callbacks. Callbacks are triggered when results are obtained or errors occur.
Code sample
#import <Foundation/Foundation.h>
#import "Synthesizer.h"
@interface Synthesizer()<NlsSpeechSynthesizerDelegate>{
IBOutlet UITextView *textViewSynthesizer;
}
@end
@implementation Synthesizer
-(void)viewDidLoad{
[super viewDidLoad];
// 1. Initialize global parameters.
// 1.1 Initialize the speech synthesis client.
_nlsClient = [[NlsClientAdaptor alloc]init];
// 1.2 Initialize the audio playback utility class.
_nlsAudioPlayer = [[NLSPlayAudio alloc]init];
// 1.3 Initialize the synthesis parameter class.
_requestParam = [[SynthesizerRequestParam alloc]init];
// 1.4 Set the log level.
[_nlsClient setLog:NULL logLevel:1];
}
-(IBAction)startSynthesizer{
// 2. Create a request object and start speech synthesis.
if(_synthesizerRequest!= NULL){
_synthesizerRequest = NULL;
}
// 2.1 Initialize the audio playback class.
[_nlsAudioPlayer cleanup];
_nlsAudioPlayer = [[NLSPlayAudio alloc]init];
// 2.2 Create a request object and set the NlsSpeechSynthesizerRequest callback.
_synthesizerRequest = [_nlsClient createSynthesizerRequest];
_synthesizerRequest.delegate = self;
// 2.3 Get the text for synthesis from the page.
NSString *inputText = [textViewSynthesizer text];
// 2.4 Set the SynthesizerRequestParam request parameters.
[_requestParam setFormat:@"pcm"];
[_requestParam setText:inputText];
[_requestParam setToken:@""];
[_requestParam setAppkey:@""];
// 2.5 Pass the request parameters.
[_synthesizerRequest setSynthesizerParams:_requestParam];
// 2.6 Start speech synthesis.
[_synthesizerRequest start];
}
/**
*3. NlsSpeechSynthesizerDelegate interface callbacks
*/
// 3.1 The current request failed.
- (void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
NSLog(@"OnTaskFailed, statusCode is: %@ error message: %@",statusCode,eMsg);
}
// 3.2 The server-side connection is closed.
- (void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
}
// 3.3 The synthesized audio data is returned in the callback. Play the data using the NLSPlayAudio utility.
- (void)OnBinaryDataReceived:(NlsDelegateEvent)event voiceData:(Byte *)data length:(NSInteger)length{
NSLog(@"Received voice data length %lu", length);
[_nlsAudioPlayer process:data length:length];
}
// 3.4 Synthesis is complete.
- (void)OnSynthesizerCompleted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
// 3.5 Synthesis starts.
- (void)OnSynthesizerStarted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
@end