Я использую этот образец здесь от Microsoft для платформы UWP.
Когда приложение работает в Hololens, мне нужно смотреть в 2D-окно, чтобы оно работало.
Если я этого не сделаю, приложение вообще не реагирует на голосовые команды.
Однако это не то, что я хочу. Я хочу, чтобы иметь возможность оглядываться вокруг при подаче голосовой команды. Подобно тому, как вы можете давать команды Кортане. В этом случае у вас нет окна приложения.
Как мне этого добиться?
Инициализация:
private async Task InitializeRecognizer(Language recognizerLanguage)
{
if (speechRecognizer != null)
{
// cleanup prior to re-initializing this scenario.
speechRecognizer.StateChanged -= SpeechRecognizer_StateChanged;
speechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed;
speechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated;
this.speechRecognizer.Dispose();
this.speechRecognizer = null;
}
try
{
this.speechRecognizer = new SpeechRecognizer(recognizerLanguage);
speechRecognizer.StateChanged += SpeechRecognizer_StateChanged;
// add list constaints here
SpeechRecognitionCompilationResult result = await speechRecognizer.CompileConstraintsAsync();
if (result.Status != SpeechRecognitionResultStatus.Success)
{
rootPage.NotifyUser( "Unable to compile grammar.",NotifyType.ErrorMessage);
}
else
{
speechRecognizer.ContinuousRecognitionSession.Completed += ContinuousRecognitionSession_Completed;
speechRecognizer.ContinuousRecognitionSession.ResultGenerated += ContinuousRecognitionSession_ResultGenerated;
}
}
catch (Exception ex)
{
if ((uint)ex.HResult == HResultRecognizerNotFound)
{
rootPage.NotifyUser( "Speech Language pack for selected language not installed.",NotifyType.ErrorMessage);
}
else
{
var messageDialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Exception");
await messageDialog.ShowAsync();
}
}
}
Способ прослушивания
public async void StartListening()
{
if (isListening == false)
{
// The recognizer can only start listening in a continuous fashion if the recognizer is currently idle.
// This prevents an exception from occurring.
if (speechRecognizer.State == SpeechRecognizerState.Idle)
{
try
{
await speechRecognizer.ContinuousRecognitionSession.StartAsync();
isListening = true;
}
catch (Exception ex)
{
var messageDialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Exception");
await messageDialog.ShowAsync();
}
}
}
else
{
isListening = false;
if (speechRecognizer.State != SpeechRecognizerState.Idle)
{
try
{
// Cancelling recognition prevents any currently recognized speech from
// generating a ResultGenerated event. StopAsync() will allow the final session to
// complete.
await speechRecognizer.ContinuousRecognitionSession.CancelAsync();
}
catch (Exception ex)
{
var messageDialog = new Windows.UI.Popups.MessageDialog(ex.Message, "Exception");
await messageDialog.ShowAsync();
}
}
}
}