📜 ⬆️ ⬇️

Talk to me, Windows Phone

Cortana has not yet become available for Russian-speaking users of Windows Phone 8.1, and this is expected soon, I suggest you make a fun April application yourself, with which you can talk to the phone, order them, and maybe play.

In fact, it turns out to be quite simple.


As an example, I suggest making a children's application that trains up to 10 on an oral account. We will write application on XAML / C #

Create an application for Windows Phone - project type Blank App (Windows Phone).
')
All permissions that need to be added to the manifest are permission to use the microphone. No other permissions needed! Open the Package.appxmanifest file, go to the “Capabilities” tab in the English-language Visual Studio or “Opportunities” in the Russian language and boldly tick the box next to the word microphone.

In the MainPage.xaml file we add a fairly simple XAML code:

<StackPanel Orientation="Vertical" VerticalAlignment="Center"> <TextBlock FontSize="48" x:Name="txtPrimer" HorizontalAlignment="Center" TextWrapping="Wrap"></TextBlock> <Button x:Name="btnOtvet" Click="btnOtvet_Click" HorizontalAlignment="Center"> !</Button> <MediaElement x:Name="mediaEl" /> </StackPanel> 

Here we have a TextBlock to display the text of the example, a button to start the game and a MediaElement, which is usually used to play audio / video, and in our case it will “pronounce” the phrases.
We get something like this:



In C # code behind (the MainPage.xaml.cs file), we add more spaces to the namespaces that are in the application by default:

 using Windows.Media.SpeechSynthesis; using Windows.Media.SpeechRecognition; 

As you can see, in the XAML code, we have an event of pressing a button. As a result, the btnOtvet_Click method is triggered. This method alone will be enough for us.

Block the button with the first line of the method (so that the user cannot call the method again until the current call is completed).

We declare a variable for voice recognition:

 SpeechRecognizer speechRecognizer = null; 

Next, we need to make a check whether the user has Russian installed:

  try { Windows.Globalization.Language lan = new Windows.Globalization.Language("ru-RU"); speechRecognizer = new SpeechRecognizer(lan); } catch { //        txtPrimer.Text = "    .    "; return; } 

Here you can see that if during the creation of an instance of SpeechRecognizer with the Russian language an error occurs as a parameter, a message will be displayed in the text field and then the code will not be continued.

The next piece of code initializes the array with values ​​from 0 to 10, after which it generates 2 random numbers in the range from 0 to 5. These numbers are displayed in the text field as an example with a + sign between them.

We create an instance of SpeechRecognitionListConstraint, passing it our array of numbers as a parameter. Our "voice engine" will be able to recognize these words. Add these words to SpeechRecognizer by calling .CompileConstraintsAsync ()
We set the options of the graphical interface UIOptions - how the voice response will be requested.

  string[] chisla = { "", "", "", "", "", "", "", "", "", "", "" }; Random rnd = new Random(); int number1 = rnd.Next(0, 6); int number2 = rnd.Next(0, 6); int result = number1 + number2; txtPrimer.Text = number1 + "+" + number2; var listConstraint = new SpeechRecognitionListConstraint(chisla, ""); speechRecognizer.Constraints.Add(listConstraint); //    await speechRecognizer.CompileConstraintsAsync(); speechRecognizer.UIOptions.ExampleText = @"  " + number1 + "+" + number2; speechRecognizer.UIOptions.AudiblePrompt = " "; speechRecognizer.UIOptions.IsReadBackEnabled = false; //      speechRecognizer.UIOptions.ShowConfirmation = false; //       

UIOptions settings are responsible for displaying such a window:



Now we recognize the spoken word:

  SpeechRecognitionResult speechRecognitionResult = await speechRecognizer.RecognizeWithUIAsync(); //   ,     if (speechRecognitionResult.Confidence == SpeechRecognitionConfidence.Rejected) { return; } string TextToSay=""; //     if (speechRecognitionResult.Text == chisla[result]) //       { TextToSay = "!"; } else { TextToSay = ""; } 

If you do not want to show the window for which we set the appearance earlier, then you can use RecognizeAsync () in the first line instead of RecognizeWithUIAsync ().

After we, such good fellows, have recognized the text and received the value “Well done!” In the TextToSay variable in the case of the correct answer and “Error” in the case of an erroneous one, we will say this result with a voice. I insert at once the whole code of play (it’s not that big):

  SpeechSynthesizer synth = new SpeechSynthesizer(); VoiceInformation ruVoice = null; SpeechSynthesisStream stream=null; try { ruVoice = (from voice in Windows.Media.SpeechSynthesis.SpeechSynthesizer.AllVoices where voice.Language == "ru-RU" select voice).First(); } catch { } if (ruVoice != null) { synth.Voice = ruVoice; stream = await synth.SynthesizeTextToStreamAsync(TextToSay); } mediaEl.SetSource(stream, stream.ContentType); mediaEl.Play(); 

What are we doing here? We declare and initialize the objects of the speech synthesizer class itself, the voice information and the stream in which we will write our phrase. Further, by the Linq query, from all the established voices we select precisely Russian (it is also possible to choose female or male). In case a voice is found, assign it to our speech synthesizer and create a stream from our text (“Well done” or “Error”). Remember in our XAML code there was an element for playing media named mediaEl? Here we set our stream as a source and start playback. I note that speech synthesis is the same with the “full-grown” full Windows 8.1 for PC and tablets, that is, when creating a universal application, the code may be common.

All that remains is to “freeze” our button:

 btnOtvet.IsEnabled = true; 

This is our method, and the entire program code is complete. It remains to test in action.


In the photo - the process of testing the application on the Lumia 1020

We decorate the application with a funny picture, we post it in the Store, we show it to friends, we make fun.

The source code of the application can be downloaded from the link .

Source: https://habr.com/ru/post/254865/


All Articles