This topic describes how to use the .NET software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction and provides installation instructions and code examples.
Prerequisites
Before you use the SDK, make sure that you have read the API reference. For more information, see API reference.
You have activated Intelligent Speech Interaction and obtained an AccessKey ID and an AccessKey secret. For more information, see Get started.
Example description
The audio file recognition example uses the `CommonRequest` method of the .NET SDK to submit recognition requests and query for results. This example uses the Remote Procedure Call (RPC) style for POP API calls.
For more information about the Alibaba Cloud .NET SDK, see Use the .NET SDK.
For more information about how to use the `CommonRequest` method of the .NET SDK, see Use CommonRequest.
SDK installation
You only need to install the core library of the Alibaba Cloud .NET SDK. You can install the library in one of the following two ways:
Add a DLL reference to the core library
Go to the .NET SDK release list. In the SDK core library section, click DLL reference for .NET 4.0 and later on the right.
In the Solution Explorer of Visual Studio, right-click My Project and then click Reference.
In the menu that appears, click Add Reference.
In the dialog box that appears, click Browse, select the downloaded DLL file, and then click OK.
Importing a project
Run the following command on the command line to download the SDK source code from GitHub.
git clone https://github.com/aliyun/aliyun-openapi-net-sdk.gitThe .NET project file for Visual Studio 2017 is `aliyun-net-sdk-core/aliyun-net-sdk-core.vs2017.csproj` and is located in the `aliyun-openapi-net-sdk` directory.
In the Visual Studio interface, right-click My Solution.
Click Add > Existing Project.
In the dialog box that appears, select the .NET project file that corresponds to your version from the source code, such as `aliyun-net-sdk-core.vs2010.csproj`, and then click Open.
Right-click your project and then click Reference > Add Reference.
Call procedure
Create and initialize an AcsClient.
Create an audio file recognition request and set the request parameters.
Submit the audio file recognition request, process the server-side response, and retrieve the task ID.
Create a request to query for the recognition result and set the query parameter to the task ID.
Poll for the recognition result.
Code example
Download the nls-sample-16k.wav file. This audio file is in the PCM format with a sample rate of 16000 Hz. In the console, the model is set to the general-purpose model. If you use a different audio file, you must specify the corresponding encoding format and sample rate in the request parameters and set the corresponding model in the console. For more information about how to set models, see Manage projects.
using System;
using Newtonsoft.Json.Linq;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Http;
namespace FileTrans
{
class FileTrans
{
// Region ID. This is a static field.
public const string REGIONID = "cn-shanghai";
public const string PRODUCT = "nls-filetrans";
public const string DOMAIN = "filetrans.cn-shanghai.aliyuncs.com";
public const string API_VERSION = "2018-08-17";
public const string POST_REQUEST_ACTION = "SubmitTask";
public const string GET_REQUEST_ACTION = "GetTaskResult";
// Request parameters
public const string KEY_APP_KEY = "appkey";
public const string KEY_FILE_LINK = "file_link";
public const string KEY_VERSION = "version";
public const string KEY_ENABLE_WORDS = "enable_words";
// Response parameters
public const string KEY_TASK = "Task";
public const string KEY_TASK_ID = "TaskId";
public const string KEY_STATUS_TEXT = "StatusText";
// Status values
public const string STATUS_SUCCESS = "SUCCESS";
public const string STATUS_RUNNING = "RUNNING";
public const string STATUS_QUEUEING = "QUEUEING";
static void Main(string[] args)
{
if (args.Length < 3)
{
System.Console.WriteLine("FileTrans Demo need params: <AccessKey Id> <AccessKey Secret> <app-key>");
return;
}
string accessKeyId = args[0];
string accessKeySecret = args[1];
string appKey = args[2];
string fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
/**
* Create an Alibaba Cloud authentication object.
*/
IClientProfile profile = DefaultProfile.GetProfile(
REGIONID,
accessKeyId,
accessKeySecret
);
DefaultAcsClient client = new DefaultAcsClient(profile);
try
{
/**
* Create an audio file recognition request and set its parameters.
*/
CommonRequest request = new CommonRequest();
request.Domain = DOMAIN;
request.Version = API_VERSION;
request.Action = POST_REQUEST_ACTION;
request.Product = PRODUCT;
request.Method = MethodType.POST;
// Set the task and add it to the request body as a JSON string.
JObject obj = new JObject();
obj[KEY_APP_KEY] = appKey;
obj[KEY_FILE_LINK] = fileLink;
// If you are a new user, use version 4.0. If you are an existing user and want to continue using the default version 2.0, comment out this parameter.
obj[KEY_VERSION] = "4.0";
// Specify whether to output word-level information. The default value is false. To enable this feature, you must set the version to 4.0.
obj[KEY_ENABLE_WORDS] = false;
string task = obj.ToString();
request.AddBodyParameters(KEY_TASK, task);
/**
* Submit the audio file recognition request and process the server-side response.
*/
CommonResponse response = client.GetCommonResponse(request);
System.Console.WriteLine(response.Data);
if (response.HttpStatus != 200)
{
System.Console.WriteLine("The audio file recognition request failed: " + response.HttpStatus);
return;
}
// Get the task ID of the audio file recognition request to query the recognition result.
string taskId = "";
JObject jsonObj = JObject.Parse(response.Data);
string statusText = jsonObj[KEY_STATUS_TEXT].ToString();
if (statusText.Equals(STATUS_SUCCESS))
{
System.Console.WriteLine("The audio file recognition request was successful!");
taskId = jsonObj[KEY_TASK_ID].ToString();
}
else
{
System.Console.WriteLine("The audio file recognition request failed!");
return;
}
/**
* Create a request to query the recognition result and set the query parameter to the task ID.
*/
CommonRequest getRequest = new CommonRequest();
getRequest.Domain = DOMAIN;
getRequest.Version = API_VERSION;
getRequest.Action = GET_REQUEST_ACTION;
getRequest.Product = PRODUCT;
getRequest.Method = MethodType.GET;
getRequest.AddQueryParameters(KEY_TASK_ID, taskId);
/**
* Submit the request to query the audio file recognition result.
* Poll for the recognition result until the server-side returns a status of "SUCCESS", "SUCCESS_WITH_NO_VALID_FRAGMENT",
* or an error. Then, stop polling.
*/
statusText = "";
while (true)
{
CommonResponse getResponse = client.GetCommonResponse(getRequest);
System.Console.WriteLine(getResponse.Data);
if (getResponse.HttpStatus != 200)
{
System.Console.WriteLine("The request to query the recognition result failed. HTTP error code: " + getResponse.HttpStatus);
break;
}
JObject jsonObj2 = JObject.Parse(getResponse.Data);
statusText = jsonObj2[KEY_STATUS_TEXT].ToString();
if (statusText.Equals(STATUS_RUNNING) || statusText.Equals(STATUS_QUEUEING))
{
// Continue polling.
System.Threading.Thread.Sleep(10 * 1000);
}
else
{
// Stop polling.
break;
}
}
if (statusText.Equals(STATUS_SUCCESS))
{
System.Console.WriteLine("The audio file was recognized successfully!");
}
else {
System.Console.WriteLine("Failed to recognize the audio file!");
}
}
catch (ServerException ex)
{
System.Console.WriteLine(ex.ToString());
}
catch (ClientException ex)
{
System.Console.WriteLine(ex.ToString());
}
}
}
}