Search code examples
c++qtsapi

C++ : How to use SAPI with QT Creator?


I've got to do a project who consist to create a web browser including a Text To Speech solution. I've used examples from the MSDN (here), but when I try to compile my project, i've got a lot of errors, and I haven't found any solutions...

I'm using QT Creator for this project.

The console output

This is my project.pro :

#-------------------------------------------------
#
# Project created by QtCreator 2015-06-18T15:14:25
#
#-------------------------------------------------

QT       += widgets webkitwidgets network multimedia

TARGET = Project

TEMPLATE = app

SOURCES += main.cpp \
    browser.cpp

HEADERS += \
    browser.h

unix|win32: LIBS += -lsapi

And my main.cpp :

#include <sapi.h>
#include "sphelper.h"

int main(int argc, char *argv[])
{
    ISpVoice *pVoice;
    pVoice = NULL;
    HRESULT hr = SpCreateBestObject(SPCAT_VOICES, L”Gender=Female”, NULL, &pVoice); 
    // --> I tried with HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);, that's the same result...
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }
    return 0;
}

Thank's for your help!


Solution

  • This is probably the most simplistic example I can think of that will work for MSVC. I don't imagine it would be too much more different for QT Creator.

    #include <sapi.h>
    #include <sphelper.h>
    #include <conio.h>
    
    int main(int argc, char *argv[]){
        HRESULT hr = S_OK;
        CComPtr<ISpVoice> cpVoice;
    
        ::CoInitialize(NULL);
    
        hr = cpVoice.CoCreateInstance(CLSID_SpVoice);
    
        if(SUCCEEDED(hr)){
            cpVoice->Speak(L"This is a test phrase.", SPF_DEFAULT, NULL);
            cpVoice.Release();
        }
    
        puts("Press any key to continue...");
        getch();
    }
    

    This code will grab the default voice and start speaking. In Windows 7 that voice will be MS Anna.