Speech for Windows Phone 8(2)

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 参考: (v=vs.105).aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/43d16c683eb63565c4e0bf60f1d14097.html