> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XSpeechSynthesizerEnumerateInstalledVoices

> XSpeechSynthesizerEnumerateInstalledVoices

# XSpeechSynthesizerEnumerateInstalledVoices

列舉已安裝的語音，並針對每個語音呼叫 *callback* 所指向的方法。

## 語法

```cpp theme={null}
HRESULT XSpeechSynthesizerEnumerateInstalledVoices(  
         void* context,  
         XSpeechSynthesizerInstalledVoicesCallback* callback  
)  
```

### 參數

*context*   \_In\_opt\_\
類型：void\*

使用者指定的變數，會傳入回呼函式，讓呼叫者能夠復原呼叫者專屬的資料。

*callback*   \_In\_\
類型：[XSpeechSynthesizerInstalledVoicesCallback](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback)\*

針對每個已安裝語音呼叫一次之方法的指標。

### 傳回值

類型：[HRESULT](https://learn.microsoft.com/openspecs/windows_protocols/ms-erref/0642cb2f-2075-4469-918c-4441e69c548a)

如果成功，則傳回 **S\_OK**；否則傳回錯誤碼。如需錯誤碼的清單，請參閱[錯誤碼](/zh-TW/reference/errorcodes)。

## 備註

<Note>在時間敏感執行緒上呼叫此函式並不安全。如需詳細資訊，請參閱[時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)。</Note>

此函式會列舉裝置上安裝的語音合成引擎 (或稱 *語音*)。此函式會針對每個已安裝的語音，叫用一次 *callback* 中指定的 [XSpeechSynthesizerInstalledVoicesCallback](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback) 回呼函式，並傳遞包含該語音相關資訊的 [XSpeechSynthesizerVoiceInformation](/zh-TW/reference/system/xspeechsynthesizer/structs/xspeechsynthesizervoiceinformation) 結構。

下列範例示範如何呼叫 **XSpeechSynthesizerEnumerateInstalledVoices** 函式來列舉裝置上已安裝的語音。此範例會叫用 **XSpeechSynthesizerInstalledVoicesCallback** 回呼函式，以 (1) 計算裝置上已安裝的語音數目，以及 (2) 將 **XSpeechSynthesizerVoiceInformation** 結構中每個已安裝語音的相關資訊輸出至偵錯工具。

```cpp theme={null}
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 主機

## 概念文件

* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

[XAccessibility](/zh-TW/reference/system/xaccessibility/xaccessibility_members)\
[XSpeechSynthesizerSetCustomVoice](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetcustomvoice)\
[XSpeechSynthesizer](/zh-TW/reference/system/xspeechsynthesizer/xspeechsynthesizer_members)


## Related topics

- [XSpeechSynthesizerEnumerateInstalledVoices](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerenumerateinstalledvoices.md)
- [XSpeechSynthesizerInstalledVoicesCallback](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback.md)
- [XSpeechSynthesizerVoiceGender](/reference/system/xspeechsynthesizer/enums/xspeechsynthesizervoicegender.md)
- [XSpeechSynthesizer](/reference/system/xspeechsynthesizer/xspeechsynthesizer_members.md)
- [XSpeechSynthesizerSetDefaultVoice](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetdefaultvoice.md)
