All Products
Search
Document Center

.NET Demo

Last Updated: Jun 02, 2020

Note:

  • Read API reference before using the SDK.
  • The SDK in this topic applies only to Intelligent Speech Interaction V2.0 that Alibaba Cloud published on July 5, 2018. If you have activated an earlier version of Intelligent Speech Interaction, activate Intelligent Speech Interaction V2.0 before using the SDK.

SDK instructions

The .NET demo of the recording file recognition service uses the CommonRequest method of the Alibaba Cloud SDK for .NET to send pctowap open platform (POP) API requests in a remote procedure call (RPC) style. You can send a recording file recognition request and query the recording file recognition result. For more information about the Alibaba Cloud SDK for .NET, see Use .NET SDK in .NET SDK Developer Guide. For more information about how to use the CommonRequest method of the .NET SDK, see Use CommonRequest in .NET SDK Developer Guide.

SDK installation

You only need to install the core library of the Alibaba Cloud SDK for .NET. You can use either of the following methods to install the .NET SDK.

Add the reference to a DLL file

  1. In the .NET SDK list, select SDK core library and click Download DLL suitable for .NET 4.0 and later to download the DLL file of the core library.
  2. In Solution Explorer of Microsoft Visual Studio, right-click your project and click References.
  3. On the drop-down menu, click Add Reference.
  4. In the dialog box that appears, click Browse, select the downloaded DLL file, and then click OK.

Add the reference to a project

  1. Run the following command to download the SDK source code from GitHub:
  1. git clone https://github.com/aliyun/aliyun-openapi-net-sdk.git

In the downloaded aliyun-openapi-net-sdk folder, open the aliyun-net-sdk-core folder and find the .NET project file. The file can be the aliyun-net-sdk-core.vs2017.csproj file (applicable to Visual Studio 2017).

  1. In Solution Explorer of Microsoft Visual Studio, right-click your solution.
  2. Choose Add > Existing Item.
  3. In the dialog box that appears, select the .NET project file, for example, aliyun-net-sdk-core.vs2010.csproj, from the downloaded source code folder, and click Open.
  4. Right-click your project and choose References > Add Reference.

Call procedure

  1. Create and initialize an AcsClient instance. Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account to authenticate the client. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.
  2. Create a recording file recognition request and set request parameters.
  3. Send the recording file recognition request and process the response returned by the server to obtain the task ID.
  4. Create a query request for the recording file recognition result and specify the task ID.
  5. Poll the recognition result.

Demo instructions

Note: The recording file in the demo is a pulse-code modulation (PCM) file sampled at 16 kHz. The demo uses a universal model in the console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the console. For more information about how to select a model, see Manage projects in Console User Guide.

nls-sample-16k.wav


Alibaba Cloud authentication:When using the SDK, you need to use your Alibaba Cloud account to authenticate the client for all API operations. You need to specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.

Sample code

  1. using System;
  2. using Newtonsoft.Json.Linq;
  3. using Aliyun.Acs.Core;
  4. using Aliyun.Acs.Core.Exceptions;
  5. using Aliyun.Acs.Core.Profile;
  6. using Aliyun.Acs.Core.Http;
  7. namespace FileTrans
  8. {
  9. class FileTrans
  10. {
  11. // The constant parameters, such as the region ID. Do not modify their values.
  12. public const string REGIONID = "ap-southeast-1";
  13. public const string PRODUCT = "nls-filetrans";
  14. public const string DOMAIN = "filetrans.ap-southeast-1.aliyuncs.com";
  15. public const string API_VERSION = "2019-08-23";
  16. public const string POST_REQUEST_ACTION = "SubmitTask";
  17. public const string GET_REQUEST_ACTION = "GetTaskResult";
  18. // The keys of request parameters.
  19. public const string KEY_APP_KEY = "appkey";
  20. public const string KEY_FILE_LINK = "file_link";
  21. public const string KEY_VERSION = "version";
  22. public const string KEY_ENABLE_WORDS = "enable_words";
  23. // The keys of response parameters.
  24. public const string KEY_TASK = "Task";
  25. public const string KEY_TASK_ID = "TaskId";
  26. public const string KEY_STATUS_TEXT = "StatusText";
  27. // The status values.
  28. public const string STATUS_SUCCESS = "SUCCESS";
  29. public const string STATUS_RUNNING = "RUNNING";
  30. public const string STATUS_QUEUEING = "QUEUEING";
  31. static void Main(string[] args)
  32. {
  33. if (args.Length < 3)
  34. {
  35. System.Console.WriteLine("FileTrans Demo need params: <AccessKey Id> <AccessKey Secret> <app-key>");
  36. return;
  37. }
  38. string accessKeyId = args[0];
  39. string accessKeySecret = args[1];
  40. string appKey = args[2];
  41. string fileLink = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav";
  42. /**
  43. * Create and authenticate a client.
  44. */
  45. IClientProfile profile = DefaultProfile.GetProfile(
  46. REGIONID,
  47. accessKeyId,
  48. accessKeySecret
  49. );
  50. DefaultAcsClient client = new DefaultAcsClient(profile);
  51. try
  52. {
  53. /**
  54. * Create a recording file recognition request and set request parameters.
  55. */
  56. CommonRequest request = new CommonRequest();
  57. request.Domain = DOMAIN;
  58. request.Version = API_VERSION;
  59. request.Action = POST_REQUEST_ACTION;
  60. request.Product = PRODUCT;
  61. request.Method = MethodType.POST;
  62. // Set parameters for the recording file recognition request and add request parameters to the request body as a JSON-formatted string.
  63. JObject obj = new JObject();
  64. obj[KEY_APP_KEY] = appKey;
  65. obj[KEY_FILE_LINK] = fileLink;
  66. // Specify the version of the recording file recognition service. If you are a new user, set this parameter to 4.0. If you use the default version 2.0, comment out this parameter.
  67. obj[KEY_VERSION] = "4.0";
  68. // Specify whether to return the recognition results of words. Default value: false. This parameter takes effect only when the version of the recording file recognition service is 4.0.
  69. obj[KEY_ENABLE_WORDS] = false;
  70. string task = obj.ToString();
  71. request.AddBodyParameters(KEY_TASK, task);
  72. /**
  73. * Send the recording file recognition request and process the response returned by the server.
  74. */
  75. CommonResponse response = client.GetCommonResponse(request);
  76. System.Console.WriteLine(response.Data);
  77. if (response.HttpStatus != 200)
  78. {
  79. System.Console.WriteLine("Error response to the recording file recognition request: " + response.HttpStatus);
  80. return;
  81. }
  82. // Obtain the ID of the recording file recognition task. You can use the task ID to query the recognition result.
  83. string taskId = "";
  84. JObject jsonObj = JObject.Parse(response.Data);
  85. string statusText = jsonObj[KEY_STATUS_TEXT].ToString();
  86. if (statusText.Equals(STATUS_SUCCESS))
  87. {
  88. System.Console.WriteLine("The recording file recognition request is successful.") ;
  89. taskId = jsonObj[KEY_TASK_ID].ToString();
  90. }
  91. else
  92. {
  93. System.Console.WriteLine("The recording file recognition request fails.") ;
  94. return;
  95. }
  96. /**
  97. * Create a query request for the recording file recognition result and specify the task ID.
  98. */
  99. CommonRequest getRequest = new CommonRequest();
  100. getRequest.Domain = DOMAIN;
  101. getRequest.Version = API_VERSION;
  102. getRequest.Action = GET_REQUEST_ACTION;
  103. getRequest.Product = PRODUCT;
  104. getRequest.Method = MethodType.GET;
  105. getRequest.AddQueryParameters(KEY_TASK_ID, taskId);
  106. /**
  107. * Send the query request for the recording file recognition result.
  108. * Use the polling method to query the recognition result. The polling runs until the status message that the server returns is SUCCESS, SUCCESS_WITH_NO_VALID_FRAGMENT,
  109. * or an error message.
  110. */
  111. statusText = "";
  112. while (true)
  113. {
  114. CommonResponse getResponse = client.GetCommonResponse(getRequest);
  115. System.Console.WriteLine(getResponse.Data);
  116. if (getResponse.HttpStatus != 200)
  117. {
  118. System.Console.WriteLine("Failed to query the recording file recognition result. HTTP status code: " + getResponse.HttpStatus);
  119. break;
  120. }
  121. JObject jsonObj2 = JObject.Parse(getResponse.Data);
  122. statusText = jsonObj2[KEY_STATUS_TEXT].ToString();
  123. if (statusText.Equals(STATUS_RUNNING) || statusText.Equals(STATUS_QUEUEING))
  124. {
  125. // Continue the polling.
  126. System.Threading.Thread.Sleep(10 * 1000);
  127. }
  128. else
  129. {
  130. // End the polling.
  131. break;
  132. }
  133. }
  134. if (statusText.Equals(STATUS_SUCCESS))
  135. {
  136. System.Console.WriteLine("The recording file is recognized.") ;
  137. }
  138. else {
  139. System.Console.WriteLine("Failed to recognize the recording file.") ;
  140. }
  141. }
  142. catch (ServerException ex)
  143. {
  144. System.Console.WriteLine(ex.ToString());
  145. }
  146. catch (ClientException ex)
  147. {
  148. System.Console.WriteLine(ex.ToString());
  149. }
  150. }
  151. }
  152. }