All Products
Search
Document Center

iOS SDK 2.0

Last Updated: Jun 02, 2020

Note:

Short sentence recognition

Short sentence recognition supports recognizing speech that lasts within 60 seconds in real time. It applies to scenarios such as voice search in applications and speech input.

SDK download

  • Download the iOS SDK
  • Use Xcode 9.3 to open the demo project in the NlsDemo.xcodeproj folder. If you use another version of Xcode, you may need to manually import the demo project.
  • The iOS SDK in the demo, that is, NlsDemo.framework or AliyunNlsSdk.framework, supports the following emulators: x86_64, ARMv7, or ARM64.
  • Apple App Store does not support the x86_64 architecture. Therefore, the iOS SDK provides an iOS device framework. You can build the following framework: NlsDemo-iOS/ReleaseFramework-iphoneOS/AliyunNlsSdk.framework.
  • AliyunNlsSdk is a dynamic library. Therefore, you need to add NlsDemo.framework or AliyunNlsSdk.framework as an embedded binary when you import files to your project.

SDK usage

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

  1. Import header files AliyunNlsClientAdaptor.h, NlsSpeechRecognizerRequest.h, and RecognizeRequestParam.h in the AliyunNlsSdk.framework directory.
  2. Call the NlsDelegate method of the NlsSpeechRecognizerRequest object.
  3. Create an NlsClient object of AliyunNlsClientAdaptor. You can globally create an NlsClient object and reuse it if necessary.
  4. Call the createRecognizeRequest method of the NlsClient object to create the RecognizeRequest object. The RecognizeRequest object cannot be reused. You need to create it for each request.
  5. Call the RecognizeRequestParam object to set recognition parameters, such as accessToken and appkey. For more information about parameters, see below.
  6. Call the setRecognizeParams method of the NlsSpeechRecognizerRequest object to pass parameters set by the RecognizeRequestParam object in step 5.
  7. Call the start method or stop method of the NlsSpeechRecognizerRequest object to start or stop recognition.
  8. Call the sendAudio:(NSData *)audioData length:(int)len method of the NlsSpeechRecognizerRequest object to send audio data to the server.
  9. If the server generates any recognition result, the callback set in step 3 is fired, returning the recognition result with a text.

Key objects

  • AliyunNlsClientAdaptor: the speech processing client, which is equivalent to a factory for all speech processing classes. You can globally create an AliyunNlsClientAdaptor instance. This object is thread-safe.
  • NlsSpeechRecognizerRequest: the request object of speech recognition. You can use this object to recognize short speeches. This object is thread-safe.
  • RecognizeRequestParam: parameters for speech recognition.
  • NlsSpeechRecognizerDelegate: the delegate that defines multiple callback functions of speech recognition. These callbacks are fired when recognition results are returned or any errors occur.

Sample code

  1. #import <Foundation/Foundation.h>
  2. #import "Recognizer.h"
  3. @interface Recognizer()<NlsSpeechRecognizerDelegate,NlsVoiceRecorderDelegate>{
  4. IBOutlet UITextView *textViewRecognize;
  5. IBOutlet UISwitch *switchRecognize;
  6. Boolean recognizerStarted;
  7. }
  8. @end
  9. @implementation Recognizer
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. // 1. Initialize global parameters.
  13. // 1.1 Initialize the client and set recognizerStarted to false.
  14. _nlsClient = [[NlsClientAdaptor alloc]init];
  15. recognizerStarted = false;
  16. // 1.2 Initialize the recorder.
  17. _voiceRecorder = [[NlsVoiceRecorder alloc]init];
  18. _voiceRecorder.delegate = self;
  19. // 1.3 Initialize the recognition object RecognizerRequestParam.
  20. _recognizeRequestParam = [[RecognizerRequestParam alloc]init];
  21. // 1.4 Specify the logging level.
  22. [_nlsClient setLog:NULL logLevel:1];
  23. }
  24. - (IBAction)startRecognize {
  25. // 2. Create the request object and start recognition.
  26. if(_recognizeRequest!= NULL){
  27. [_recognizeRequest releaseRequest];
  28. _recognizeRequest = NULL;
  29. }
  30. // 2.1 Create the request object and set the NlsSpeechRecognizerDelegate object.
  31. _recognizeRequest = [_nlsClient createRecognizerRequest];
  32. _recognizeRequest.delegate = self;
  33. // 2.2 Set request parameters for the RecognizerRequestParam object.
  34. [_recognizeRequestParam setServiceUrl:@"wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1"];
  35. [_recognizeRequestParam setFormat:@"opu"];
  36. [_recognizeRequestParam setEnableIntermediateResult:true];
  37. // Obtain a dynamic token. For more information, see https://www.alibabacloud.com/help/doc-detail/72153.htm.
  38. // <AccessKeyId><AccessKeySecret>Use your Alibaba Cloud account to generate the AccessKey ID and AccessKey secret in Alibaba Cloud console (https://ak-console.aliyun.com/).
  39. [_recognizeRequestParam setToken:@""];
  40. // Obtain an appkey in the Intelligent Speech Interaction console (https://nls-portal.console.aliyun.com/).
  41. [_recognizeRequestParam setAppkey:@""];
  42. // 2.3 Pass request parameters.
  43. [_recognizeRequest setRecognizeParams:_recognizeRequestParam];
  44. // 2.4 Start recording and recognition, and set recognizerStarted to true.
  45. [_voiceRecorder start];
  46. [_recognizeRequest start];
  47. recognizerStarted = true;
  48. // 2.5 Update the UI.
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. // The code for UI update.
  51. [self->switchRecognize setOn:true];
  52. self->textViewRecognize.text = @"start Recognize!" ;
  53. });
  54. }
  55. - (IBAction)stopRecognize {
  56. // 3. Stop recognition and recording, and stop the recognition request.
  57. [_voiceRecorder stop:true];
  58. [_recognizeRequest stop];
  59. recognizerStarted = false;
  60. _recognizeRequest = NULL;
  61. }
  62. /**
  63. * 4. Set NlsSpeechRecognizerDelegate callbacks.
  64. */
  65. // 4.1 The recognition callback that is fired when the request fails.
  66. -(void)OnTaskFailed:(NlsDelegateEvent)event statusCode:(NSString*)statusCode errorMessage:(NSString*)eMsg{
  67. NSLog(@"OnTaskFailed, error message is: %@",eMsg);
  68. }
  69. // 4.2 The recognition callback that is fired when the server is disconnected.
  70. -(void)OnChannelClosed:(NlsDelegateEvent)event statusCode:(NSString*)statusCode errorMessage:(NSString*)eMsg{
  71. NSLog(@"OnChannelClosed, statusCode is: %@",statusCode);
  72. [_voiceRecorder stop:true];
  73. }
  74. // 4.3 The recognition callback that is fired when the recognition result is returned and the recognition is completed.
  75. -(void)OnRecognizedCompleted:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString*)statusCode errorMessage:(NSString*)eMsg{
  76. recognizerStarted = false;
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. // The code for UI update.
  79. self->textViewRecognize.text = result;
  80. NSLog(@"%@", result);
  81. [self->switchRecognize setOn:false];
  82. });
  83. }
  84. // 4.4 The recognition callback that is fired when the intermediate result is returned.
  85. -(void)OnRecognizedResultChanged:(NlsDelegateEvent)event result:(NSString *)result statusCode:(NSString*)statusCode errorMessage:(NSString*)eMsg{
  86. dispatch_async(dispatch_get_main_queue(), ^{
  87. // The code for UI update.
  88. NSLog(@"%@", result);
  89. self->textViewRecognize.text = result;
  90. });
  91. }
  92. /**
  93. * 5. Set recording callbacks.
  94. */
  95. - (void)recorderDidStart {
  96. NSLog(@"Did start recorder!");
  97. }
  98. - (void)recorderDidStop {
  99. NSLog(@"Did stop recorder!");
  100. }
  101. - (void)voiceDidFail:(NSError *)error {
  102. NSLog(@"Did recorder error!");
  103. }
  104. // 5.1 The recording data callback.
  105. - (void)voiceRecorded:(NSData *)frame {
  106. if (_recognizeRequest != nil &&recognizerStarted) {
  107. // Send the data returned by the recording thread to the server.
  108. [_recognizeRequest sendAudio:frame length:(short)frame.length];
  109. }
  110. }
  111. @end