Search code examples
windows-phone-8cortana

Windows Cortana always listen from inside app


The new update to windows cortana, has an always listen mode, similar to Google's "OK Google" command, allowing users to activate Cortana even when the phone is on standby. It's "hey Cortana".

In the same way when my app is launched, I want to have an always listen mode, where it can listen to only specific set of words( just like "hey Cortana"), and respond to it, accordingly.


Solution

  • You can achieve continuous dictation using ContinuousRecognitionSession for Windows 10.

    private SpeechRecognizer speechRecognizer;
    private CoreDispatcher dispatcher;
    private StringBuilder dictatedTextBuilder;
    
    this.dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
    this.speechRecognizer = new SpeechRecognizer();
    SpeechRecognitionCompilationResult result =
    
    await speechRecognizer.CompileConstraintsAsync();
    speechRecognizer.ContinuousRecognitionSession.ResultGenerated +=
    ContinuousRecognitionSession_ResultGenerated;
    
    private async void ContinuousRecognitionSession_ResultGenerated(
    SpeechContinuousRecognitionSession sender,
    SpeechContinuousRecognitionResultGeneratedEventArgs args)
    {
    
    if (args.Result.Confidence == SpeechRecognitionConfidence.Medium ||
      args.Result.Confidence == SpeechRecognitionConfidence.High)
      {
        dictatedTextBuilder.Append(args.Result.Text + " ");
    
        await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
          dictationTextBox.Text = dictatedTextBuilder.ToString();
          btnClearText.IsEnabled = true;
        });
      }
    else
    {
      await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
          dictationTextBox.Text = dictatedTextBuilder.ToString();
        });
    }
    }
    

    Here is the complete example