Speech Recognition In Windows Phone 8

In this article we will learn how to use speech recognition in Windows Phone 8.

Introduction

In this article we will learn how to use speech recognition in Windows Phone 8. Speech recognition is a way to get input from the user. Once we have recognized the text spoken by the user we can use that text to perform various operations like activating a new app or writing it in a text area.

Speech Reorganization in Windows Phone 8

Speech recognition uses an async programming model so we need  to ensure that we must wrap the call to this API in an Async method. The simplest way of enabling an App for speech recognition involves the following procedure:

  1. Add the following using statement:

    using Windows.Phone.Speech.Recognition;
     
  2. Create a new instance of SpeechRecognizerUI class as in the following:

    SpeechRecognizerUI mic = newSpeechRecognizerUI();
     
  3. Show the recognizer UI to the user and start speech processing as in the following:

    SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();
     
  4. Get the result of the recognition if it is not null or empty as in the following:

    res.RecognitionResult.Text
     

The following is the full code snippet illustrating the preceding.

XAML

<phone:PhoneApplicationPage

    x:Class="Demo.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

 

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

 

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock Text="Speech Recognition" Margin="9,-7,0,0" FontSize="40"/>

        </StackPanel>

 

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid.RowDefinitions>

                <RowDefinition Height="243*"/>

                <RowDefinition Height="406*"/>

            </Grid.RowDefinitions>

            <Button Name="startBtn" Content="Start" Click="strtClick" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="225"/>

            <Button Name="popBtn" Content="Clear" Click="clear" HorizontalAlignment="Left" Margin="225,10,0,0" VerticalAlignment="Top" Width="221"/>

            <TextBlock Name="Result" Margin="0,87,0,153" Grid.RowSpan="2"></TextBlock>

        </Grid>

    </Grid>

 

</phone:PhoneApplicationPage>

C# Code Behind
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Navigation;

using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

using Demo.Resources;

using System.Threading;

using Windows.Phone.Speech.Recognition;

 

namespace Demo

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }

   

        private async void strtClick(object sender, RoutedEventArgs e)

        {

            SpeechRecognizerUI mic = new SpeechRecognizerUI();

            SpeechRecognitionUIResult res = await mic.RecognizeWithUIAsync();

            if (res != null)

            {

                Result.Text = res.RecognitionResult.Text;

            }

        }

 

        private void clear(object sender, RoutedEventArgs e)

        {

            Result.Text = "";

        }

    }

}

The code above will launch the Speech recognition task. For the code above to work you need to provide your app the following two capabilities:

  1. ID_CAP_SPEECH_RECOGNITION
     
  2. ID_CAP_MICROPHONE

If you don't provide the following two permissions then your app will crash due to the lack of permissions to access the hardware.

 

 
 
 
 
 
 
Summary

That's all for this article. I hope you have found it useful. In case of any doubt feel free ask in the comments section.