XSpeechSynthesizerEnumerateInstalledVoices

枚举安装的语音,并调用由针对每个语音的 callback 指向的方法。

语法

HRESULT XSpeechSynthesizerEnumerateInstalledVoices(  
         void* context,  
         XSpeechSynthesizerInstalledVoicesCallback* callback  
)  

参数

context _In_opt_
类型:void*

一个用户指定的变量,将其传入回调函数,以便调用方可以恢复调用方特定的数据。

callback _In_
类型:XSpeechSynthesizerInstalledVoicesCallback*

指向将要调用的方法的指针,对每个安装的语音调用一次。

返回值

类型:HRESULT

如果成功,则返回 S_OK;否则返回错误代码。 有关错误代码的列表,请参阅错误代码

备注

注意

在时间敏感线程上调用此函数是不安全的。 有关详细信息,请参阅时间敏感线程

此函数将枚举安装在设备上的语音合成引擎或 voices。 该函数为每个已安装的语音调用一次在 callback 函数中指定的 XSpeechSynthesizerInstalledVoicesCallback 回调函数,然后它传递包含有关该语音的信息的 XSpeechSynthesizerVoiceInformation 结构。

下面的示例演示如何调用 XSpeechSynthesizerEnumerateInstalledVoices 函数来枚举设备上已安装的语音。 该示例调用 XSpeechSynthesizerInstalledVoicesCallback 回调函数以执行以下操作:(1) 计算在设备上安装的语音数目,以及 (2) 将有关每个已安装语音的信息从 XSpeechSynthesizerVoiceInformation 结构输出到调试程序。

int Game::CountInstalledVoices()
{
    HRESULT hr = S_OK;
    int* voiceCount = new int();
    // Call XSpeechSynthesizerEnumerateInstalledVoices with a callback function
    // defined as a lambda expression that increments the count of installed voices 
    // each time it's invoked, and outputs information about each voice to the 
    // debugger for display.
    hr = XSpeechSynthesizerEnumerateInstalledVoices(voiceCount,
        [](
            _In_ const XSpeechSynthesizerVoiceInformation* information,
            _In_ void* context
            ) -> bool 
        {
        // Increment and update the count of installed voices.
        int* voiceCount = (int*)context;
        *voiceCount = *voiceCount + 1;
        CopyMemory(context, voiceCount, sizeof(int));

        // Output information about the current installed voice
        // to the debugger for display.
        char buffer[1000];
        int bufferLen;
        bufferLen = sprintf(buffer, "(%s) %s - %s, %s\n",
            information->VoiceId,
            information->Description,
            information->Gender == XSpeechSynthesizerVoiceGender::Male ? "Male" : "Female",
            information->Language);
        OutputDebugStringA(buffer);
      
        // Continue iterating through installed voices.
        // If this was set to false, the calling function would stop
        // enumerating installed voices.
        return true;
        }
    );

    // Return the count of installed voices.
    return *voiceCount;
}

要求

头文件:XSpeechSynthesizer.h

库:xgameruntime.lib

支持平台:Windows、Xbox One 系列主机和 Xbox Series 主机

另请参阅

XAccessibility
XSpeechSynthesizerSetCustomVoice
XSpeechSynthesizer