windows phone 8 語音 - Speech for Windows Phone 8


最近有很多人咨詢我關於 windows phone 8 語音識別方面的用法,今天我就在這里給大家總結一下以便大家學習交流

在windows phone8中語音可以理解為三部分功能即: 語音控制 voice commands, 語音識別 speech recognition, 文字語音 text-to-speech (TTS)。

在寫程序之前要先把你的WP8 聲明成支持Voice command的APP

 

1. 語音控制 voice commands 對應 ID_CAP_SPEECH_RECOGNITION, ID_CAP_MICROPHONE, and ID_CAP_NETWORKING capabilities in the app manifest file

 

語音控制顧名思義可以使用語音命令來操作應用程程序,包括啟動和頁面跳轉。

首先你要現在你的WP8項目中添加一個VCD文件,它的用途就是來聲明你的WP8App 可以接受那些 語音命令並且會給用戶那些語音反饋以及會執行那些動作。

例如: CommandPrefix 就是聲明你的應用程序打開關鍵字 Command Name 聲明你的應用可以識別執行那些動作 Navigate 可以將包含關鍵字的命令導航到特定頁面。

 

當然你還要在你的系統中注冊你的應用是一個支持語音控制的應用 這里MSDN推薦用一個單獨的方法承載

 

         public async  void InitCommand()
        {
            await VoiceCommandService.InstallCommandSetsFromFileAsync( new Uri( " ms-appx:///VoiceCommandDefinition1.xml "));
        }

         //  Code to execute when the application is launching (eg, from Start)
        
//  This code will not execute when the application is reactivated
         private  void Application_Launching( object sender, LaunchingEventArgs e)
        {
            InitCommand();
        }

 

既然你的應用可以支持語音的目標導航當然在我們的目標頁面也是可以拿到用戶的語音參數的,當然請放心這里我們的SDK已經幫我們吧語音翻譯成文字了,相信大家處理文字還會很得心應手的.

你只重寫一下你的目標頁面的 onNavigatedTo事件

 

         protected  override  void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
             base.OnNavigatedTo(e);

             //  Is this a new activation or a resurrection from tombstone?
             if (e.NavigationMode == System.Windows.Navigation.NavigationMode.New)
            {

                 //  Was the app launched using a voice command?
                 if (NavigationContext.QueryString.ContainsKey( " voiceCommandName "))
                {

                     //  If so, get theon name of the voice command.
                     string voiceCommandName
                      = NavigationContext.QueryString[ " voiceCommandName "];

                     //  Define app actions for each voice command name.
                     switch (voiceCommandName)
                    {
                         case  " showWidgets ":
                             string viewWidget = NavigationContext.QueryString[ " widgetViews "];

                             //  Add code to display specified widgets.
                             break;

                         //  Add cases for other voice commands. 
                         default:

                             //  There is no match for the voice command name.
                            onbreak;
                    }
                }
            }
        }

 

msdn參考: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206959(v=vs.105).aspx

 

2. 語音識別 speech recognition 對應 ID_CAP_SPEECH_RECOGNITION, ID_CAP_MICROPHONE, and ID_CAP_NETWORKING capabilities in the app manifest

 

語音識別分為兩種

2.1 基於本地的語音識別

本地語音識別不需要訪問網絡 但是只能識別個別字對於功能性操作已經夠了下面給大家一個例子

這里提到了 Exception from HRESULT: 0x800455BC 

有不少朋友問我這個問題是怎么回事兒? 這里請注意我的 rengionizer 設置是 zh-cn, 因為操作系統大家習慣選擇中文 然而默認rengionizer 是英文的 很多朋友都卡在這個問題上了 這里我特意提一下。

這段代碼就可以再沒有網絡的情況下識別你說出的中文數字。

 

            recoWithUI =  new SpeechRecognizerUI();
            SpeechRecognitionUIResult recoResult;

             try
            {
                 //  Query for a recognizer that recognizes French as spoken in France.
                IEnumerable<SpeechRecognizerInformation> frenchRecognizers =  from recognizerInfo  in InstalledSpeechRecognizers.All
                                                                              where recognizerInfo.Language ==  " zh-CN "
                                                                              select recognizerInfo;
                 //  Set the recognizer to the top entry in the query result.
                recoWithUI.Recognizer.SetRecognizer(frenchRecognizers.ElementAt( 0));

                 //  Create a string array of China numbers.
                 string[] nombres = {  " "" "" "" "" "
                          " "" "" "" "" " };

                 //  Create a list grammar from the string array and add it to the grammar set.
                recoWithUI.Recognizer.Grammars.AddGrammarFromList( " ChinaNumbers ", nombres);

                 //  Display text to prompt the user's input.
                recoWithUI.Settings.ListenText =  " Say a china number ";

                 //  Give an example of ideal speech input.
                recoWithUI.Settings.ExampleText =  "  '一', '二', '三', '四'  ";

                 //  Load the grammar set and start recognition.
                recoResult = await recoWithUI.RecognizeWithUIAsync();
            }
             catch (Exception ex)
            {
                 // Exception from HRESULT: 0x800455BC
                 string ext = ex.Message;
                 return;
            }

             //  Do something with the recognition result.
            MessageBox.Show( string.Format( " You said {0}. ", recoResult.RecognitionResult.Text));

 

2.2 基於網絡的語音識別

當然用戶可以隨便說點兒什么 windows phone 8 可以使用網絡進行一個雲識別 從實現上來說稍有不同 但也很簡單我也寫了一個demo給大家參考

這里我注釋掉的code是另外一種沒有UI的識別過程 請大家根據自己App需求來定吧,這里是初始化了一個基於網絡的語音識別器 SpeechPredefinedGrammar.WebSearch

         //  Constructor
         public MainPage()
        {
            InitializeComponent();
            
             // SpeechRecognizer myRecognizer = new SpeechRecognizer();

            
// recoWithUI.Grammars.AddGrammarFromPredefinedType("citySearch", SpeechPredefinedGrammar.WebSearch);

            
//  Initialize the SpeechRecognizer and add the WebSearch grammar.
            recoWithUI =  new SpeechRecognizerUI();

            recoWithUI.Recognizer.Grammars.AddGrammarFromPredefinedType( " citySearch ", SpeechPredefinedGrammar.WebSearch);

             //  Prompt the user for a city name.
            displayText.Text =  " What's your destination city? ";

             //  Subscribe to the AudioCaptureStateChanged event.
            recoWithUI.Recognizer.AudioCaptureStateChanged += myRecognizer_AudioCaptureStateChanged;
        }

 由於需要訪問網絡 所以是一個異步的訪問過程 所以注冊了一個 myRecognizer_AudioCaptureStateChanged 的返回事件

 

         //  Detect capture state changes and write the capture state to the text block.
         void myRecognizer_AudioCaptureStateChanged(SpeechRecognizer sender, SpeechRecognizerAudioCaptureStateChangedEventArgs args)
        {
             if (args.State == SpeechRecognizerAudioCaptureState.Capturing)
            {
                 this.Dispatcher.BeginInvoke( delegate { displayText.Text =  " Listening "; });
            }
             else  if (args.State == SpeechRecognizerAudioCaptureState.Inactive)
            {
                 this.Dispatcher.BeginInvoke( delegate { displayText.Text =  " Thinking "; });
            }
        }

最后我通過一個按鈕來激發這個語音識別的過程

         private async  void Button_Click_1( object sender, RoutedEventArgs e)
        {
             //  Start recognition.
            SpeechRecognitionUIResult recoResult = await recoWithUI.RecognizeWithUIAsync();

             //  Do something with the recognition result.
            MessageBox.Show( string.Format( " You said {0}. ", recoResult.RecognitionResult.Text));

             /// / Check to see if speech input was rejected and prompt the user.
             // if (recoResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.Rejected)
            
// {
            
//     displayText.Text = "Sorry, didn't catch that. \n\nSay again.";
            
// }

             /// / Check to see if speech input was recognized with low confidence and prompt the user to speak again.
             // else if (recoResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.Low)
            
// {
            
//     displayText.Text = "Not sure what you said. \n\nSay again.";
            
// }

             /// / Check to see if speech input was recognized and confirm the result.
             // else if (recoResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.High ||
            
//       recoResult.RecognitionResult.TextConfidence == SpeechRecognitionConfidence.Medium)
            
// {
            
//     displayText.Text = "Heard you say: \n\n" + recoResult.RecognitionResult.Text;
            
// }
        }

MSDN 參考: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206963(v=vs.105).aspx

 

3. 文字語音 text-to-speech (TTS) 對應  ID_CAP_SPEECH_RECOGNITION capability in the app manifest

最后 TTS 大家很容易理解的 就是把文字轉換成聲音念出來很簡單

 

private async  void ButtonSimpleTTS_Click( object sender, RoutedEventArgs e)
{
  SpeechSynthesizer synth =  new SpeechSynthesizer();
    
  await synth.SpeakTextAsync( " You have a meeting with Peter in 15 minutes. ");
}

當然也同樣支持語言的選擇

//  Declare the SpeechSynthesizer object at the class level.
SpeechSynthesizer synth;

//  Handle the button click event.
private async  void SpeakFrench_Click_1( object sender, RoutedEventArgs e)
{
   //  Initialize the SpeechSynthesizer object.
  synth =  new SpeechSynthesizer();

   //  Query for a voice that speaks French.
  IEnumerable<VoiceInformation> frenchVoices =  from voice  in InstalledVoices.All
                      where voice.Language ==  " fr-FR "
                      select voice;
            
   //  Set the voice as identified by the query.
  synth.SetVoice(frenchVoices.ElementAt( 0));

   //  Count in French.
  await synth.SpeakTextAsync( " un, deux, trois, quatre ");

這里我就是直接粘貼的MSDN代碼 真的很簡單就不廢話了

連接給出 : http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207057(v=vs.105).aspx

 

如果有什么問題或者建議請在新浪微博上 @王博_Nick


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM