C # call Baidu speech recognition demo

Source: Internet
Author: User

using system;using system.collections.generic;using system.componentmodel;using  system.data;using system.drawing;using system.text;using system.windows.forms;using  system.net;using system.io;using system.threading;using newtonsoft.json.linq;namespace  voice{    public partial class form1 : form     {        string testtoken =  "";         string cuid =  "";//Casual Writing          string serverURL =  "Http://vop.baidu.com/server_api";         public form1 ()         {             initializecomponent ();             cuid&nbsP;= guid.newguid (). ToString ();        }         Private string post (String audiofilepath)         {             serverURL +=  "?lan=zh& Cuid=kwwwvagaa&token= " + testtoken;             filestream fs = new filestream (Audiofilepath, filemode.open);             byte[] voice = new byte[fs. Length];            fs. Read (Voice, 0, voice. Length);             fs. Close ();             fs. Dispose ();             httpwebrequest request = null;             uri uri = new uri (ServerURL);             request =  (HttpWebRequest) WebRequest.Create ( URI);             request. Timeout = 5000;            request. method =  "POST";             request. contenttype =  "audio/wav; rate=16000";             request. Contentlength = voice. length;            try             {                using  (stream writestream = request. GetRequestStream ())                  {                     writestream.write (Voice, 0, voice. Length);                     writestream.close ();                     writestream.dispose ();                 }             }            catch             {                return null;             }             string result = string. empty;            string result_final =  string. empty;            using  (HttpWebResponse  response =  (HttpWebResponse) request. GetResponse ())             {                 using  (stream  Responsestream = response. GetResponseStream ())                  {                    using  (streamreader readstream = new  StreamReader (Responsestream, encoding.utf8))                      {                         string  Line = string. empty;                         stringbuilder sb = new stringbuilder ();                          while  (!readstream.endofstream)                          {                              line = readstream.readline ();                              SB. Append (line);                             SB. Append ("\ r");                         }                         readstream.close ();                        &Nbsp; readstream.dispose ();                         RESULT = SB. ToString ();                         string[] indexs = result. Split (', ');                         foreach  (String index in indexs)                           {                             string[] _ Indexs = index. Split (' "');                             if  (_indexs[2] ==  ": [")                                   result_final = _indexs[3];                         }                      }                     responsestream.close ();                     responsestream.dispose ();                 }                 response. Close ();            }             return result_final;         }        private void button1_click (object  Sender, eventargs e)         {             textbox1.text = post (application.startuppath +  " \\2.wav ");         }    }}

The attachment has the source code, the specific look inside the Readme

C # call Baidu speech recognition demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.