首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SAPI (Microsoft ) CoCreateInstance失败

SAPI (Microsoft ) CoCreateInstance失败
EN

Stack Overflow用户
提问于 2014-03-06 17:04:39
回答 1查看 1.1K关注 0票数 2

我正在尝试从Microsoft示例页中运行一个SAPI示例。

当我运行应用程序(使用VS2010)时,这一行失败:

代码语言:javascript
复制
hr = cpVoice.CoCreateInstance( CLSID_SpVoice );

hr返回错误代码,所有其他代码都不会执行。

我不知道为什么我错了,因为我认为正确地使用页面中的示例代码,而且我以前从未使用过这个API。

这是我的完整main.cpp文件。我错过了什么?

代码语言:javascript
复制
#include "stdafx.h"
#include <sapi.h>
#include <sphelper.h>
#include <atlcomcli.h>

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = S_OK;
    CComPtr <ISpVoice>      cpVoice;
    CComPtr <ISpStream>     cpStream;
    CSpStreamFormat         cAudioFmt;

    //Create a SAPI Voice
    hr = cpVoice.CoCreateInstance( CLSID_SpVoice );

    //Set the audio format
    if(SUCCEEDED(hr))
    {
        hr = cAudioFmt.AssignFormat(SPSF_22kHz16BitMono);
    }

    //Call SPBindToFile, a SAPI helper method,  to bind the audio stream to the file
    if(SUCCEEDED(hr))
    {

        hr = SPBindToFile( L"c:\\ttstemp.wav",  SPFM_CREATE_ALWAYS,
            &cpStream, & cAudioFmt.FormatId(),cAudioFmt.WaveFormatExPtr() );
    }

    //set the output to cpStream so that the output audio data will be stored in cpStream
    if(SUCCEEDED(hr))
    {
        hr = cpVoice->SetOutput( cpStream, TRUE );
    }

    //Speak the text "hello world" synchronously
    if(SUCCEEDED(hr))
    {
        hr = cpVoice->Speak( L"Hello World",  SPF_DEFAULT, NULL );
    }

    //close the stream
    if(SUCCEEDED(hr))
    {
        hr = cpStream->Close();
    }

    //Release the stream and voice object    
    cpStream.Release ();
    cpVoice.Release();

    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-06 17:49:16

在使用CoInitialize[Ex] API之前,必须使用CoCreateInstance初始化线程。您得到的错误代码应该明确地建议:CO_E_NOTINITIALIZED (您应该将它发布在您的问题上!)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22231176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档