> ## 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.

# XSpeechSynthesizerCreate

> XSpeechSynthesizerCreate

# XSpeechSynthesizerCreate

建立語音合成器。

## 語法

```cpp theme={null}
HRESULT XSpeechSynthesizerCreate(  
         XSpeechSynthesizerHandle* speechSynthesizer  
)  
```

### 參數

*speechSynthesizer*   \_Out\_\
類型：XSpeechSynthesizerHandle\*

所建立之語音合成器的控制代碼。

### 傳回值

類型：[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>

使用此函式來建立並接收新語音合成器執行個體的控制代碼，該執行個體可存取已安裝之語音合成引擎 (或稱 *語音*) 的功能。

根據預設，新的語音合成器執行個體會使用目前的系統語音。若要列舉並取得目前裝置上已安裝語音的相關資訊，請使用 [XSpeechSynthesizerEnumerateInstalledVoices](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerenumerateinstalledvoices) 函式，並搭配 [XSpeechSynthesizerInstalledVoicesCallback](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerinstalledvoicescallback) 回呼函式。對於每個已安裝的語音，[XSpeechSynthesizerVoiceInformation](/zh-TW/reference/system/xspeechsynthesizer/structs/xspeechsynthesizervoiceinformation) 結構會提供語音識別碼、描述、顯示文字、性別、語言及其他資訊。叫用 [XSpeechSynthesizerSetCustomVoice](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetcustomvoice) 以使用不同的已安裝語音，或叫用 [XSpeechSynthesizerSetDefaultVoice](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizersetdefaultvoice) 以再次使用目前的系統語音。

建立語音合成器控制代碼並指定語音之後，請使用 [XSpeechSynthesizerCreateStreamFromText](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext) 函式來建立語音合成器資料流，並從純文字合成語音。使用 [XSpeechSynthesizerGetStreamDataSize](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdatasize) 和 [XSpeechSynthesizerGetStreamData](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizergetstreamdata) 函式，從語音合成器資料流取得合成語音的音訊資料，然後在所有未完成的非同步作業完成之後，使用 [XSpeechSynthesizerCloseStreamHandle](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosestreamhandle) 函式關閉語音合成器資料流。

使用完語音合成器之後，請使用 [XSpeechSynthesizerCloseHandle](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) 函式關閉語音合成器並釋放系統資源。

若要防止記憶體流失，請在完成所有使用語音合成器控制代碼的作業之後，呼叫 [XSpeechSynthesizerCloseHandle](/zh-TW/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle) 函式來關閉該控制代碼。

下列範例示範如何使用語音合成器和已安裝的語音從純文字合成語音，以及如何使用語音合成器資料流合成音訊資料。**XSpeechSynthesizerCreate** 和 **XSpeechSynthesizerSetCustomVoice** 函式會建立語音合成器執行個體，並在 *voiceId* 中指定了語音識別碼時，選擇性地為其指派自訂語音。接著，**XSpeechSynthesizerCreateStreamFromText** 函式會建立語音合成器資料流，並從 *textToSpeak* 中指定的純文字合成語音。建立資料流之後，**XSpeechSynthesizerGetStreamDataSize** 和 **XSpeechSynthesizerGetStreamData** 函式會從資料流擷取要播放之合成語音的音訊資料。最後，在音訊資料播放完成之後，**XSpeechSynthesizerCloseStreamHandle** 和 **XSpeechSynthesizerCloseHandle** 函式會關閉語音合成器資料流和語音合成器。

```cpp theme={null}
HRESULT Game::SynthesizeSpeech(
    const char* textToSpeak,
    const char* voiceId)
{
    // Create a new speech synthesizer.
    XSpeechSynthesizerHandle ssHandle = nullptr;
    if (FAILED(XSpeechSynthesizerCreate(&ssHandle))) { return E_FAIL; }

    // If a voice ID was specified, attempt to set the speech synthesizer to
    // use the specified voice. Note that voiceId has a default value of nullptr, 
    // as specified in its function declaration.
    if (voiceId != nullptr) 
    {
        if (FAILED(XSpeechSynthesizerSetCustomVoice(ssHandle, voiceId))) { return E_FAIL; }
    }

    // Create a new speech synthesizer stream from the specified text.
    XSpeechSynthesizerStreamHandle ssStreamHandle = nullptr;
    if (FAILED(XSpeechSynthesizerCreateStreamFromText(ssHandle, textToSpeak, &ssStreamHandle))) { return E_FAIL; }

    // Get the size of the buffer needed for the audio data from our stream.
    size_t bufferSize;
    if (FAILED(XSpeechSynthesizerGetStreamDataSize(ssStreamHandle, &bufferSize))) { return E_FAIL; }

    // Define the buffer, then retrieve the audio data from our stream.
    std::vector<char> streamData;
    streamData.resize(bufferSize);
    if (FAILED(XSpeechSynthesizerGetStreamData(ssStreamHandle, bufferSize, streamData.data(), &bufferSize))) { return E_FAIL; }

    // We now have audio data from the speech synthesizer stream, so let's play it. 
    // For the purposes of this example, the sound is played synchronously, so that we don't
    // risk having an outstanding asynchronous operation when we close the stream.
    PlaySoundW(reinterpret_cast<LPCWSTR>(streamData.data()), nullptr, SND_MEMORY);

    // We're done with the speech synthesizer stream, so let's close it.
    if (FAILED(XSpeechSynthesizerCloseStreamHandle(ssStreamHandle))) { return E_FAIL; }

    // We're done with the speech synthesizer, so let's close that, too.
    if (FAILED(XSpeechSynthesizerCloseHandle(ssHandle))) { return E_FAIL; }

    return S_OK;
}
```

## 需求

**標頭：** XSpeechSynthesizer.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 概念文件

* [文字轉換語音](/zh-TW/build/console-features/text-to-speech/text-to-speech)
* [時間敏感執行緒](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/console-dev/overviews/threads/time-sensitive-threads)

## 另請參閱

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


## Related topics

- [XSpeechSynthesizerCreate](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreate.md)
- [XSpeechSynthesizerCreateStreamFromSsml](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromssml.md)
- [XSpeechSynthesizerCreateStreamFromText](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizercreatestreamfromtext.md)
- [XSpeechSynthesizerCloseHandle](/reference/system/xspeechsynthesizer/functions/xspeechsynthesizerclosehandle.md)
- [XSpeechSynthesizer](/reference/system/xspeechsynthesizer/xspeechsynthesizer_members.md)
