1

I'm trying to develop a speech-to-text-input with the Windows.Media.SpeechRecognition namespace as described here: https://msdn.microsoft.com/en-us/library/windows.media.speechrecognition.aspx

Now, I'm working in Qt and apparently, there's no such thing as a Windows namespace:

using namespace Windows::Media::SpeechRecognition;

int main(int argc, char *argv[])
{
    SpeechRecognizer speechRecognizer = new SpeechRecognizer();

    //...

    return 0;
}

results in

C2653: "Windows" no class or namespace

Okay, so I figured I might have to include something or add a library to my pro-file, but I can't find anything about what and where to get it.

In case the question comes up: I'm using this, because I need the speech input to accept languages other than English only.

Saftkeks
  • 181
  • 2
  • 15

1 Answers1

3

You need to include Windows.Media.SpeechRecognition.h header. From desktop apps, the namespace is actually ABI::Windows::Media::SpeechRecognition. You'll also need to use WRL to call it. You can create the SpeechRecognizer object like this:

#include <roapi.h>
#include <Windows.Media.SpeechRecognition.h>
#include <wrl.h>

using namespace ABI::Windows::Media::SpeechRecognition;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

int main()
{
    HRESULT hr = RoInitialize(RO_INIT_MULTITHREADED);
    if (FAILED(hr))
        return hr;

    ComPtr<IInspectable> instance;
    ComPtr<ISpeechRecognizer> recognizer;
    hr = RoActivateInstance(HStringReference(L"Windows.Media.SpeechRecognition.SpeechRecognizer").Get(), &instance);
    if (FAILED(hr))
        return hr;

    hr = instance.As(&recognizer);
    if (FAILED(hr))
         return hr;

    // do stuff with recognizer

    RoUninitialize();
}

You'll also need to link with RuntimeObject.lib for it to find functions like RoInitialize or RoActivateInstance.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Sunius
  • 2,789
  • 18
  • 30