concurrency::task AppMain::StartRecognizeSpeechCommands()

in Tools/Recorder/AppMain.cpp [195:274]


  concurrency::task<bool> AppMain::StartRecognizeSpeechCommands()
  {
    return StopCurrentRecognizerIfExists().then([this]()
    {
      if (!InitializeSpeechRecognizer())
      {
        return concurrency::task_from_result<bool>(false);
      }

      // Here, we compile the list of voice commands by reading them from the map.
      Platform::Collections::Vector<Platform::String^>^ speechCommandList =
        ref new Platform::Collections::Vector<Platform::String^>();

      speechCommandList->Append(L"start");
      speechCommandList->Append(L"stop");

      Windows::Media::SpeechRecognition::SpeechRecognitionListConstraint^ spConstraint =
        ref new Windows::Media::SpeechRecognition::SpeechRecognitionListConstraint(
          speechCommandList);

      _speechRecognizer->Constraints->Clear();
      _speechRecognizer->Constraints->Append(spConstraint);

      return concurrency::create_task(
        _speechRecognizer->CompileConstraintsAsync()).
        then([this](concurrency::task<Windows::Media::SpeechRecognition::SpeechRecognitionCompilationResult^> previousTask)
      {
        try
        {
          Windows::Media::SpeechRecognition::SpeechRecognitionCompilationResult^ compilationResult =
            previousTask.get();

          if (compilationResult->Status == Windows::Media::SpeechRecognition::SpeechRecognitionResultStatus::Success)
          {
            // If compilation succeeds, we can start listening for results.
            return concurrency::create_task(
              _speechRecognizer->ContinuousRecognitionSession->StartAsync()).
              then([this](concurrency::task<void> startAsyncTask)
            {
              try
              {
                // StartAsync may throw an exception if your app doesn't have Microphone permissions. 
                // Make sure they're caught and handled appropriately (otherwise the app may silently not work as expected)
                startAsyncTask.get();
                return true;
              }
              catch (Platform::Exception^ exception)
              {
                dbg::trace(
                  L"Exception while trying to start speech recognition: %s",
                  exception->Message->Data());

                return false;
              }
            });
          }
          else
          {
            dbg::trace(
              L"Could not initialize constraint-based speech engine!");

            // Handle errors here.
            return concurrency::create_task([this] { return false; });
          }
        }
        catch (Platform::Exception^ exception)
        {
          // Note that if you get an "Access is denied" exception, you might need to enable the microphone 
          // privacy setting on the device and/or add the microphone capability to your app manifest.

          dbg::trace(
            L"Exception while trying to initialize speech command list: %s",
            exception->Message->Data());

          // Handle exceptions here.
          return concurrency::create_task([this] { return false; });
        }
      });
    });
  }