public string api_id = "8888"; Your id public string api_record = null; public string Api_record_format = "wav"; public string api_record_hz = "16000"; public string api_key = "Geovindu"; Your key public string api_secret_key = "55519"; Your Secrret_key public string api_language = "zh"; public string api_access_token = null; public string Strjson = ""; Recording [DllImport ("Winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)] public static extern int mciSendString (String Lpstrcommand, string lpstrreturnstring, int ureturnlength, int hwnd Callback); <summary>//////</summary> public Form1 () {InitializeComponent ( ); }//<summary>///</summary>//<param name= "Sender" ></param> <param name= "E" ></param> private void Form1_Load (object sender, EventArgs e) {api_record=application.startuppath + "\ \ work. wav"; Api_access_token = Getstraccess (Api_key, Api_secret_key); }//<summary>///</summary>//<param name= "Sender" ></param> <param name= "E" ></param> private void Button1_Click (object sender, EventArgs e) { This.textBox2.Text = Getstrtext (api_id, Api_access_token, Api_language, Api_record, Api_record_format, Api_record_ HZ); }///<summary>///</summary>/<param name= "Para_api_key" > Your key< /param>//<param name= "Para_api_secret_key" > Your secrret_key</param>//<returns></r Eturns> public string getstraccess (string Para_api_key, String para_api_secret_key) {//Method parameter description ://para_api_key:api_key (your key) Para_api_secret_key (your Secrret_key)//Method return Value Description://Baidu Authentication Password code , access_token string access_html = null; string access_token = null; String getaccessurl = "Https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + Para_api_key + "&client_secret=" + para_api_secret_key; try {HttpWebRequest getaccessrequest = WebRequest.Create (Getaccessurl) as HttpWebRequest; Getaccessrequest.proxy = null; Getaccessrequest.contenttype = "Multipart/form-data"; getaccessrequest.accept = "*/*"; Getaccessrequest.useragent = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "; Getaccessrequest.timeout = 30000;//30 Second connection is not successful interrupt Getaccessrequest.method = "POST"; HttpWebResponse Response = Getaccessrequest.getresponse () as HttpWebResponse; using (StreamReader strhttpcomback = new StreamReader (response. GetResponseStream (), Encoding.UTF8)) {access_html = Strhttpcomback.readtoend (); }} catch (WebException ex) {MessageBox.Show (ex). ToString ()); } jobject Jo = Jobject.parse (access_html); Access_token = jo["Access_token"]. ToString ();//Get returned toke return access_token; }//<summary>///</summary>/<param name= "para_api_id" ></param >//<param name= "Para_api_access_token" ></param>//<param name= "Para_api_language" > </param>//<param name= "Para_api_record" ></param>//<param name= "Para_format" >&L t;/param>//<param name= "Para_hz" ></param>//<returns></returns> public string Getstrtext (string para_api_id, String PARA_API_ACC Ess_token, String para_api_language, String Para_api_record, String Para_format, String para_hz) {//Square Method parameter Description://para_api_id:api_id (your id)//para_api_access_token (getstraccess (...) method to get the Access_token password)//para_api_language (the language you want to recognize, ZH,EN,CT)//para_api_record (the path to the voice file) Para_format (format of voice file)//para_hz (voice file sample rate 16000 or 8000)//The method returns a value: The method performs the correct return value is the text of the speech translation, error is the error number, you can go to see the Baidu voice document, see the corresponding error string strText = null; string error = NULL; FileInfo fi = new FileInfo (Para_api_record); FileStream fs = new FileStream (Para_api_record, FileMode.Open); Byte[] Voice = new Byte[fs. Length]; Fs. Read (voice, 0, voice. Length); Fs. Close (); StRing Gettexturl = "http://vop.baidu.com/server_api?lan=" + para_api_language + "&cuid=" + para_api_id + "&token=" + Para_api_access_token; HttpWebRequest Gettextrequst = WebRequest.Create (Gettexturl) as HttpWebRequest; /* Gettextrequst.proxy = NULL; GetTextRequst.ServicePoint.Expect100Continue = false; GetTextRequst.ServicePoint.UseNagleAlgorithm = false; GetTextRequst.ServicePoint.ConnectionLimit = 65500; gettextrequst.allowwritestreambuffering = false;*/Gettextrequst.contenttype = "Audio/" +para_format+ "; rate= "+PARA_HZ; Gettextrequst.contentlength = fi. Length; Gettextrequst.method = "POST"; gettextrequst.accept = "*/*"; Gettextrequst.keepalive = true; Gettextrequst.useragent = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 2.0.50727) "; Gettextrequst.timeout = 30000;//30 Second connection not successfully interrupted using (Stream Writestream = Gettextrequst.getrequeststream ()) {Writestream.write (voice, 0, Voice. Length); } HttpWebResponse Gettextresponse = Gettextrequst.getresponse () as HttpWebResponse; using (StreamReader Strhttptext = new StreamReader (Gettextresponse.getresponsestream (), Encoding.UTF8)) { Strjson = Strhttptext.readtoend (); } jobject jsons = Jobject.parse (Strjson);//Parse JSON if (jsons["err_m SG "]. value<string> () = = "success.") {StrText = jsons["Result"][0]. ToString (); return strText; } else {error = jsons["Err_no"]. Value<string> () + jsons["Err_msg"]. Value<string> (); return error; } }
Reference: HTTP://YUYIN.BAIDU.COM/DOCS/ASR/57
API Request Method Basic description
- Speech Recognition Interface supports POST mode
- Currently the API only supports the whole segment of speech recognition mode, that is, the need to upload entire speech to identify
- There are two ways to upload voice data: Implicit send and display send
- The original voice recording format currently only supports the evaluation of Mono voice with 8k/16k sample rate of 16bit bit depth
- Compression format support: PCM (uncompressed), WAV, opus, Speex, AMR, X-flac
- System support Language types: Chinese (en), Cantonese (CT), English (en)
- Official address: Http://vop.baidu.com/server_api
CSharp: Baidu speech recognition