Speech recognition in WP (I): basic recognition

Source: Internet
Author: User

Many of WP 8.1's content is still in an unknown state. Therefore, the speech recognition mentioned in this article is based on WP8 and similar in 8.1. It is also implemented using runtime APIs, if you do not know what the Runtime API is, it does not affect learning and development, because after VS creates a project, it will reference all the libraries by default.

In this article, we will start with a simple implementation. The next article will introduce how to design your own voice commands.

Let's talk about the location of the database. The speech recognition-related API is in the windows. Phone. Speech. Recognition namespace. You can guess 99.999998% by name. In this namespace, there are two classes that can be used to complete speech recognition:

A. speechrecognizer

B. speechrecognizerui

The functions of the two classes are actually equivalent. The class with the UI end will display an identification interface during identification. You will see it when you look at the examples. For the speechrecognizer class, the recognizeasync method is called. After an asynchronous wait, an instance of speechrecognitionresult is returned, where the text attribute indicates the recognized text. For the speechrecognizerui class, the notify method can be called for identification, after an asynchronous wait, the speechrecognitionuiresult instance is returned. The speechrecognitionresult is referenced in the recognitionresult attribute of the instance to obtain the recognized text.

The following is an example.

1. Start Vs and create a new project named "My tall application ".

2. Add the following XAML to the default mainpage. XAML page.

<Grid X: Name = "contentpanel" grid. row = "1" margin = "12, 0, 12, 0"> <stackpanel> <button content = "start to recognize" fontsize = "40"/> <textbox X: name = "txtinput" Height = "250" margin = ","/> </stackpanel> </GRID>

3. Add the Click Event Processing Method to the button control.

<Button content = "start to recognize" fontsize = "40" Click = "onclick"/>

4. Go to the Code view and first use the namespace we will use.

using Windows.Phone.Speech.Recognition;

5. Add the following code to The onclick method.

Private async void onclick (Object sender, routedeventargs e) {// instantiate speechrecognizer sr = new speechrecognizer (); // start to recognize button BTN = (button) sender; BTN. isenabled = false; speechrecognitionresult result = await Sr. recognizeasync (); // display recognized text txtinput. TEXT = result. text; BTN. isenabled = true ;}

6. In principle, we have completed the example above. However, there is also a small step to open the configuration file wmappmanifest. xml and add the following functional requirements:
Id_cap_networking: networks are allowed. Enabling speech recognition is required.

Id_cap_microphone: It's all about speech recognition. How can there be fewer microphones?

Id_cap_speech_recognition: enables applications to support speech recognition.

That is:

    <Capabilities>      <Capability Name="ID_CAP_NETWORKING" />      <Capability Name="ID_CAP_MEDIALIB_AUDIO" />      <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />      <Capability Name="ID_CAP_SENSORS" />      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />      <Capability Name="ID_CAP_MICROPHONE" />      <Capability Name="ID_CAP_SPEECH_RECOGNITION" />    </Capabilities>

Now, make sure that your mobile phone is connected to the Internet. We recommend that you connect to wi-fi, because the cost of tiger network traffic is high.
Well, don't hesitate to run it. When you see that the program is running, click the start recognition button, and then submit your confession to your mobile phone. After a while, the recognition result will be displayed in the text box on the page, as shown in.

Alas, at this time, it is estimated that everyone will say that I am a liar. Yes, an unexpected error is reported. Although I dare not say that I have reported an error, at least after my test, an exception will occur most of the time, this may be the reason why the system's dictation mode does not yet provide friendly support for Chinese characters. It's okay. Since the dictation mode is not good, let's search for the mode.
In the above onclick, the code is changed to the following:

Private async void onclick (Object sender, routedeventargs e) {// instantiate speechrecognizer sr = new speechrecognizer (); Sr. grammars. addgrammarfrompredefinedtype ("CNS", speechpredefinedgrammar. websearch); // start identification

That is to say, this line is added:

Sr. grammars. addgrammarfrompredefinedtype ("CNS", speechpredefinedgrammar. Websearch );

The central component is a key name that can be retrieved at will. This line of code tells the recognition system: "I want to use the search mode". The search mode can recognize Chinese characters. After such a change, run it again, and then you confess to your phone. Good guy, wait until the result is finished.

Ah, it's okay.

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.