Use the Baidu Speech API for speech recognition.

Source: Internet
Author: User

Because of the project needs, these days are trying to use the Baidu Speech API for speech recognition. but the recognition is "Ah, oh" or something, I cried.

Here I just share this process, the error is that the post voice data is now the piece, it may be a conversion problem it.

API Request Address:: Http://vop.baidu.com/server_api

Voice upload mode: Show send: put voice data directly in Http-body

Other parameters: CUiD: User id,token: Key, LAN: language, etc.
To learn more please see the official documentation: Http://developer.baidu.com/wiki/index.php?title=docs/cplat/media/voice

Implementation of the steps: 1, recording, save the file 2, get token 3, post data and request to get the return value

1. Recording

String FileName ="Test.wav";PrivateMediaCapture _mediacapturemanager;PrivateStorageFile _recordstoragefile;Private Async voidRecord_click (Objectsender, RoutedEventArgs e) //Click to start recording {Try{//Create a file in Temp folder, and replace it if it exists_recordstoragefile =awaitApplicationData.Current.TemporaryFolder.CreateFileAsync (FileName, creationcollisionoption.replaceexisting); //The key is these two sentences .Mediaencodingprofile recordprofile = mediaencodingprofile.createwav (Audioencodingquality.auto);//Recording-wav format       await_mediacapturemanager.startrecordtostoragefileasync (Recordprofile, This. _recordstoragefile);//Save the recording to the file you created}Catch(Exception ex) {Debug.WriteLine (ex).  Message.tostring ()); }}Private Async voidStop_click (Objectsender, RoutedEventArgs e) {await_mediacapturemanager.stoprecordasync ();//Stop Recording}

2. Get token

Private voidGetToken () {HttpWebRequest request= (HttpWebRequest) webrequest.create ("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id="+"Api_key of Application"+"&client_secret="+"Secret_key of Application"); Request.        BeginGetResponse (Responsetokencall, request); }        Private voidResponsetokencall (IAsyncResult result) {Try{HttpWebRequest HttpWebRequest=(HttpWebRequest) result.                asyncstate; WebResponse WebResponse=httpwebrequest.endgetresponse (Result); using(Stream stream =WebResponse.GetResponseStream ())using(StreamReader reader =NewStreamReader (Stream)) {                    stringContent =Reader.                    ReadToEnd (); stringSSSS = content. Replace ("\"",""). Replace ("{",""). Replace ("}",""). Replace ("\ n",""); string[] Indexs = ssss. Split (','); foreach(stringIndexinchIndexs) {                        string[] _indexs = index. Split (':'); if(_indexs[0] =="Access_token") Token= _indexs[1];//Token acquired                    }                }            }            Catch(Exception ex) {Debug.WriteLine ("failed to get token"); }        }

3. Send Request

Private Async voidPost () {stringServerURL ="Http://vop.baidu.com/server_api?"; HttpWebRequest Request= (HttpWebRequest) webrequest.create (ServerURL +"lan=zh&cuid="+ cuid +"&token="+ token);//cuid = guid.newguid ().  ToString (); The official recommended use of MAC address/Phone IMEI and other similar parameters, as long as the only goodRequest. Continuetimeout =10000;//timed outRequest. Method ="POST";//POST Request//Post DataRequest. BeginGetRequestStream (responsestreamcallbackpost, request);//Post Data        }        Private Async voidresponsestreamcallbackpost (IAsyncResult result) {StorageFile StorageFile=awaitApplicationData.Current.TemporaryFolder.GetFileAsync (FileName);//Remove the saved audio files from the Temp folderIBuffer Buffer=awaitFileio.readbufferasync (StorageFile); //Read files to Ibuffer byte[] Voice = Windowsruntimebufferextensions.toarray (buffer,0, (int) buffer. Length);//convert Ibuffer to byte[]            
HttpWebRequest HttpWebRequest =(HttpWebRequest) result. asyncstate; Httpwebrequest.contenttype="audio/wav;rate=8000";//parameter Settings using(Stream Writestream =Httpwebrequest.endgetrequeststream (Result)) {Writestream.write (voice,0, Voice. Length);//Write} httpwebrequest.begingetresponse (Responsecall, HttpWebRequest);//Send Request } Private voidResponsecall (IAsyncResult result) {Try{HttpWebRequest HttpWebRequest=(HttpWebRequest) result. asyncstate; WebResponse WebResponse=httpwebrequest.endgetresponse (Result); using(Stream stream =WebResponse.GetResponseStream ())using(StreamReader reader =NewStreamReader (Stream)) { stringContent = Reader. ReadToEnd ();//the UTF-8 encoding is returned stringA = regex.unescape (content);//to convert } } Catch(Exception ex) {Debug.WriteLine ("Recognition Failure"); } }

Token was acquired once and was said to expire after 30 days.
During the process, a problem was encountered. I asked GetToken () and post () to deal with it, but found that token was not available after post, which led to the request URL without tokens.

So we have to get the token before we do it.

Additionally (HttpWebRequest) request cannot assign a value to the Content-length property.

Use the Baidu Speech API for speech recognition.

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.