All Products
Search
Document Center

Intelligent Speech Interaction:iOS SDK

Last Updated:Mar 03, 2023

The real-time speech recognition service provides an SDK for iOS. This topic describes how to download and install the SDK. This topic also provides sample code for you to use the SDK.

Note

We recommend that you use the latest version of the SDK for iOS. The current version is no longer updated. For more information, see NUI SDK for iOS.

Prerequisites

  • You understand how the SDK works. For more information, see Overview.

  • A project is created in the Intelligent Speech Interaction console and the appkey of the project is obtained. For more information, see Create a project.

  • An access token that is used to call Intelligent Speech Interaction services is obtained. For more information, see Obtain an access token.

Download and installation

  1. Download the SDK for iOS.

    Decompress the downloaded ZIP package to obtain the NlsDemo-iOS folder. The demo project is stored in NlsDemo-iOS/NlsDemo.

  2. Double-click the NlsDemo.xcodeproj folder and use Xcode to open the demo project.

    • The SDK for iOS in the demo project is stored in NlsDemo/AliyunNlsSdk.framework. The SDK for iOS supports the following emulators: x86_64, ARMv7, or ARM64.

    • To submit and publish an application to Apple App Store, build the following framework: NlsDemo-iOS/dynamic-verison-framework/AliyunNlsSdk.framework.

    • The sample code for real-time speech recognition contains the following two code files:

      • Transcriber.m: demonstrates how to import an audio file to the SDK for recognition.

      • TranscriberwithRecorder.m: demonstrates how to use the SDK to record and recognize audio data.

Call procedure

Note

To import the SDK for iOS to your project, add destination files as embedded binaries.

  1. Import the header files in the AliyunNlsSdk.framework/Headers directory, including AliyunNlsClientAdaptor.h, NlsSpeechTranscriberRequest.h, and TranscriberRequestParam.h.

  2. Call the NlsSpeechTranscriberDelegate method of the NlsSpeechTranscriberRequest object.

  3. Create an nlsClient object of AliyunNlsClientAdaptor. You can globally create an nlsClient object for all requests.

  4. Call the createTranscriberRequest method of the nlsClient object to create the NlsSpeechTranscriberRequest object. The NlsSpeechTranscriberRequest object cannot be reused. You must create an object for each request.

  5. Call the TranscriberRequestParam object to set recognition parameters, such as the access token and appkey.

  6. Call the setTranscriberParams method of the NlsSpeechTranscriberRequest object to pass parameters that are set by the TranscriberRequestParam object in Step 5.

  7. Call the start method or stop method of the NlsSpeechTranscriberRequest object to start or stop recognition.

  8. Call the sendAudio:(NSData *)audioData length:(int)len method of the NlsSpeechTranscriberRequest object to send audio data to the server.

  9. If the server generates any recognition result, the callback that is set in Step 3 is fired, which returns the recognition result in text format.

Key objects

  • AliyunNlsClientAdaptor: the speech processing client. You can use this client to process short sentence recognition, real-time speech recognition, and speech synthesis tasks. The client is thread-safe. We recommend that you globally create one AliyunNlsClientAdaptor instance.

  • NlsSpeechTranscriberRequest: the request object of speech recognition. You can use this object to recognize speech data streams. This object is thread-safe.

  • TranscriberRequestParam: the parameters for speech recognition.

  • NlsSpeechTranscriberDelegate: the delegate that defines multiple callback functions of speech recognition. Callbacks are fired when recognition results are returned or any errors occur.

Sample code

#import <Foundation/Foundation.h>
#import "Transcriber.h"
@interface Transcriber()<NlsSpeechTranscriberDelegate,NlsVoiceRecorderDelegate>{
    IBOutlet UITextView *textViewTranscriber;
    IBOutlet UISwitch *switchTranscriber;
    Boolean transcriberStarted;
}
@end
@implementation Transcriber
-(void)viewDidLoad {
    [super viewDidLoad];
    //1. Initialize global parameters.
    // 1.1 Initialize the client and set transcriberStarted to false.
    _nlsClient = [[NlsClientAdaptor alloc]init];
    transcriberStarted = false;
    // 1.2 Initialize the recorder.
    _voiceRecorder = [[NlsVoiceRecorder alloc]init];
    _voiceRecorder.delegate = self;
    // 1.3 Initialize the recognition object TranscriberRequestParam.
    _transRequestParam = [[TranscriberRequestParam alloc]init];
    // 1.4 Specify the logging level.
    [_nlsClient setLog:NULL logLevel:1];
}
- (IBAction)startTranscriber {
    //2. Create the request object and start recognition.
    if(_transcriberRequest! = NULL){
        [_transcriberRequest releaseRequest];
        _transcriberRequest = NULL;
    }
    // 2.1 Create the request object and set the NlsSpeechTranscriberDelegate object.
    _transcriberRequest = [_nlsClient createTranscriberRequest];
    _transcriberRequest.delegate = self;
    // 2.2 Set request parameters for the TranscriberRequestParam object.
    [_transRequestParam setFormat:@"opu"];
    [_transRequestParam setEnableIntermediateResult:true];
    [_transRequestParam setToken:@""];
    [_transRequestParam setAppkey:@""];
    // 2.3 Pass request parameters.
    [_transcriberRequest setTranscriberParams:_transRequestParam];
    // 2.4 Start recording and recognition, and set transcriberStarted to true.
    [_voiceRecorder start];
    [_transcriberRequest start];
    transcriberStarted = true;
    // 2.5 Update the UI.
    dispatch_async(dispatch_get_main_queue(), ^{
        // The code for UI update.
        [self->switchTranscriber setOn:true];
        self->textViewTranscriber.text = @"start Recognize!" ;
    });
}
- (IBAction)stopTranscriber {
    // 3. Stop recognition and recording, and stop the recognition request.
    [_voiceRecorder stop:true];
    [_transcriberRequest stop];
    transcriberStarted= false;
    _transcriberRequest = NULL;
    dispatch_async(dispatch_get_main_queue(), ^{
        // The code for UI update.
        [self->switchTranscriber setOn:false];
    });
}
/**
 *4. Set NlsSpeechTranscriberDelegate callbacks.
 */
// 4.1 This recognition callback is fired when the request fails.
- (void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnTaskFailed, statusCode is: %@ error message :%@",statusCode,eMsg);
}
// 4.2 This recognition callback is fired when the server is disconnected.
- (void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
}
// 4.3 This recognition callback is fired when the recognition starts.
- (void)OnTranscriptionStarted:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
// 4.4 This recognition callback is fired when a sentence begins.
- (void)OnSentenceBegin:(NlsDelegateEvent)event result:(NSString*)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    dispatch_async(dispatch_get_main_queue(), ^{
        // The code for UI update.
        self->textViewTranscriber.text = result;
        NSLog(@"%@", result);
    });
}
// 4.5 This recognition callback is fired when a sentence ends.
- (void)OnSentenceEnd:(NlsDelegateEvent)event result:(NSString*)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    dispatch_async(dispatch_get_main_queue(), ^{
        // The code for UI update.
        self->textViewTranscriber.text = result;
        NSLog(@"%@", result);
    });
}
// 4.6 This recognition callback is fired when the intermediate result is returned.
- (void)OnTranscriptionResultChanged:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
    dispatch_async(dispatch_get_main_queue(), ^{
        // The code for UI update.
        self->textViewTranscriber.text = result;
        NSLog(@"%@", result);
    });
}
// 4.7 This recognition callback is fired when the recognition result is returned and the recognition is completed.
- (void)OnTranscriptionCompleted:(NlsDelegateEvent)event statusCode:(NSString *)statusCode errorMessage:(NSString *)eMsg {
}
/**
 *5. Set recording callbacks.
 */
- (void)recorderDidStart {
    NSLog(@"Did start recorder!") ;
}
- (void)recorderDidStop {
    NSLog(@"Did stop recorder!") ;
}
- (void)voiceDidFail:(NSError *)error {
    NSLog(@"Did recorder error!") ;
}
// 5.1 The recording data callback.
- (void)voiceRecorded:(NSData *)frame {
    if (_transcriberRequest ! = nil && transcriberStarted) {
        // Send the data that is returned by the recording thread to the server.
        if ([_transcriberRequest sendAudio:frame length:(short)frame.length] == -1) {
            NSLog(@"connect closed ,stop transcriberRequest!") ;
            [self stopTranscriber];
        }
    }
}
@end