Pregunta

Please have a look at the following code

main.cpp

    #define _ATL_APARTMENT_THREADED

#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override something,
//but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

#include <sapi.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "Hello" << endl;
    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
        return FALSE;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        cout << "Succeeded" << endl;
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }
    else
    {
        cout << "Not succeeded" << endl;
    }

    ::CoUninitialize();
    return TRUE;
}

When I run this code, the window opens, prints the "Hello" message. but no sound is coming out! It supposed to speak "Hello World" ! Why is this?

Following is the QT .pro settings in case it is required

Following is the QT .pro settings

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-03T14:31:00
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = Speech
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Bin"
INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Include"



LIBS += "C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386/sapi.lib"
LIBS += "C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib/User32.lib"
¿Fue útil?

Solución 3

This issue is a driver error or application conflict. My laptop is Dell inspiron 4030 and it is the one where this issue happens. Works in my Desktop PC.

Otros consejos

Try with SPF_ASYNC instead of 0 while calling pVoice->Speak(L"Hello world", 0, NULL).
Here is how I do it :

[...]   

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, reinterpret_cast<void**>(&pVoice));
if( SUCCEEDED( hr ) )
{        

    const wchar_t* reqAttributs = L"Language=409"; // 409 = en_US; 809 = en_UK; 40C = fr_FR
    const wchar_t* optAttributs = L"Gender=Female"; // or L"Gender=Male"

    ISpObjectToken* cpTokenEng;
    if (FAILED(::SpFindBestToken(SPCAT_VOICES, reqAttributs, optAttributs, &cpTokenEng))) {
        throw std::exception("Couldn't find a Token with the required attributs.");
    }

    pVoice->SetVoice(cpTokenEng);

    hr = pVoice->Speak(L"Hello World", SPF_ASYNC, nullptr);
        if (hr == S_OK) {
            // OK

        } else if (hr == E_INVALIDARG) {
            // One or more parameters are invalid

        } else if (hr == E_POINTER) {
            // Invalid pointer

        } else if (hr == E_OUTOFMEMORY) {
            // Exceeded available memory

        } else {
            // Unknown error
        }

        hr = pVoice->WaitUntilDone(INFINITE);

        pVoice->Release();
        pVoice = nullptr;
}

[...]

The Token part is not really necessary but it can be useful if you want to customize the voice (Several voices must be installed on your computer for optimal result).

You're calling Release on the pVoice object before the speech has had time to finish. The API has a waitUntilDone function, that would probably be needed before releasing the object.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top