使SAPI TTS说出字符串变量

时间:2013-05-05 04:01:48

标签: c++ text-to-speech sapi

所以,我有一个程序,我使用这样的SAPI:

ISpVoice * pVoice = NULL;

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

HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void     **)&pVoice);
if( SUCCEEDED( hr ) )
{
    hr = pVoice->Speak(L"I can talk!", SPF_IS_XML, NULL);
}

但我想声明一个变量然后让sapi说出来。我该怎么做?

由于

1 个答案:

答案 0 :(得分:0)

使用std::wstring。假设您希望用户输入要说的内容(禁止文本框和按钮):

std::cout << "Enter lines of text to speak:\n";
for (std::wstring text; std::wcin >> text;) {
    if (FAILED(hr = pVoice->Speak(text.c_str(), SPF_IS_XML, NULL))) {
        std::cout << "Sorry, the text could not be spoken. The error code is " << hr << '\n';
    }
}