Public Static stringGetstraccess (stringPara_api_key,stringPara_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 stringaccess_html =NULL; stringAccess_token =NULL; stringGetaccessurl ="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) asHttpWebRequest; //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 seconds If the connection is unsuccessfulGetaccessrequest.method ="Post"; HttpWebResponse Response= Getaccessrequest.getresponse () asHttpWebResponse; using(StreamReader Strhttpcomback =NewStreamReader (response. GetResponseStream (), Encoding.UTF8)) {access_html=Strhttpcomback.readtoend (); } } Catch(WebException ex) {Console.Write (ex). Message); Console.ReadLine (); } jobject Jo=Jobject.parse (access_html); Access_token= jo["Access_token"]. ToString ();//get back the Toke returnAccess_token; } Public Static stringGetstrtext (stringPARA_API_ID,stringPara_api_access_token,stringPara_api_language,stringPara_api_record,stringPara_format,stringpara_hz) { stringStrjson =""; //method Parameter Description://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 stringStrText =NULL; stringError =NULL; FileInfo fi=NewFileInfo (Para_api_record); FileStream FS=NewFileStream (Para_api_record, FileMode.Open); byte[] Voice =New byte[FS. Length]; Fs. Read (voice,0, Voice. Length); Fs. Close (); stringGettexturl ="http://vop.baidu.com/server_api?lan="+ Para_api_language +"&cuid="+ para_api_id +"&token="+Para_api_access_token; HttpWebRequest Gettextrequst= WebRequest.Create (Gettexturl) asHttpWebRequest; /*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 seconds If the connection is unsuccessful using(Stream Writestream =Gettextrequst.getrequeststream ()) {Writestream.write (voice,0, Voice. Length); } HttpWebResponse gettextresponse= Gettextrequst.getresponse () asHttpWebResponse; using(StreamReader Strhttptext =NewStreamReader (Gettextresponse.getresponsestream (), Encoding.UTF8)) {Strjson=Strhttptext.readtoend (); } jobject jsons= Jobject.parse (Strjson);//parsing JSON if(jsons["err_msg"]. value<string> () = ="success.") {StrText= jsons["result"][0]. ToString (); returnStrText; } Else{Error= jsons["Err_no"]. value<string> () + jsons["err_msg"]. value<string>(); returnerror; } } stringAccess = Getstraccess ("VJDS2SNHUUDLZ57KTYTQDAVJ","896348e496b47382d49e0cd2e7d5f1e8"); Console.Write (access); Console.ReadLine (); stringText = Getstrtext ("d0-27-88-67-21-4b", Access,"ZH","1.wav","wav","8000");
Note: The video sampling rate only supports 8000,16000
C # Baidu API speech recognition