c++ - 在 Windows 服务中使用 SAPI 进行简单的文本转语音

标签 c++ sapi

为什么仅当进程作为服务运行时,以下代码在 Speak() 之后失败,错误代码为 0x80045063 (SPERR_NOT_ACTIVE_SESSION)?

ISpVoicePtr pVoice;
CoCreateInstance(
    CLSID_SpVoice,
    0,
    CLSCTX_INPROC_SERVER,
    IID_ISpVoice, ( LPVOID * ) &pVoice
);

pVoice->SetOutput( 0, TRUE );
pVoice->Speak( L"Hello", 0, NULL );

我能想到的唯一资源是关于与桌面 session 交互的服务的安全更改。 .NET 中的以下内容将在服务中工作,所以我很好奇如何使用带或不带 SAPI 的纯 C++ 来完成它。

System::Speech::Synthesis::SpeechSynthesizer synth;
synth.SetOutputToDefaultAudioDevice();
synth.Speak( "Hello" );

最佳答案

SAPI 文档将 SPERR_NOT_ACTIVE_SESSION 列为

Neither audio output nor input is supported for non-active console sessions.

服务在后台 session 中运行。由于 Session 0 Isolation在 Vista 中引入,服务无法访问交互式资源,其中包括文本转语音。

谷歌搜索,我发现了一些支持该说法的评论:

http://www.ip-symcon.de/en/service/documentation/module-reference/text-to-speech/tts-speak/

This function does not work under Windows __ Vista/2003__ or newer. This is because services such as IP-Symcon, for security reasons, can not access interactive components in the system. The Text To Speech output is such an interactive component. The problem manifests itself with the error message: __ OLE error 80045063__ extract MSDN: SPERR_NOT_ACTIVE_SESSION.

http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.tapi/2009-06/msg00003.html

Note that they broke ( deliberately I think ) this stuff beginning on Vista (and later). If you try to use SAPI's ISpMMSysAudio interface in a service (as they sketch in their bullet points in your link above), sooner or later you'll see a SPERR_NOT_ACTIVE_SESSION error.

至于 SpeechSynthesizer,我的猜测是它在内部不使用 SAPI。 SpeechSynthesizer 的源代码可在此处获得:

SpeechSynthesizer.cs

VoiceSynthesis.cs

关于c++ - 在 Windows 服务中使用 SAPI 进行简单的文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293808/

相关文章:

visual-c++ - SAPI 5.3 语音转文本

c++ - "objects map"在 opengl 中

c++ - 如何避免 QDial 控件中的回绕?

c++ - begin() 和 rend() 有什么区别?

c++ - 无法在 CListCtrl 中编辑标签

C++ 和微软 SAPI 5 : How to list all available voices and select a voice

c++ - 使用新的内存分配失败

c# - 使用 Phonics Sounds 的文字转语音

sapi - 使用 SAPI 将音频(wav 文件)转换为文本?

c# - Microsoft Speech Platform 11(服务器)上的 AppendDictation?