Share via


SpeechSynthesizer 類別

定義

針對文字轉換語音 (TTS) 服務,提供已安裝語音合成引擎 (語音) 功能的存取權。

public ref class SpeechSynthesizer sealed : IClosable
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class SpeechSynthesizer final : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class SpeechSynthesizer final : IClosable
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class SpeechSynthesizer : System.IDisposable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class SpeechSynthesizer : System.IDisposable
function SpeechSynthesizer()
Public NotInheritable Class SpeechSynthesizer
Implements IDisposable
繼承
Object Platform::Object IInspectable SpeechSynthesizer
屬性
實作

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

下列範例示範如何從基本文字字串產生語音音訊資料流程。

// The media object for controlling and playing audio.
MediaElement mediaElement = this.media;

// The object for controlling the speech synthesis engine (voice).
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

// Generate the audio stream from plain text.
SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World");

// Send the stream to the media object.
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
// The object for controlling the speech synthesis engine (voice).
synth = ref new SpeechSynthesizer();
// The media object for controlling and playing audio.
media = ref new MediaElement();
// The string to speak.
String^ text = "Hello World";

// Generate the audio stream from plain text.
task<SpeechSynthesisStream ^> speakTask = create_task(synth->SynthesizeTextToStreamAsync(text));
speakTask.then([this, text](SpeechSynthesisStream ^speechStream)
{
    // Send the stream to the media object.
    // media === MediaElement XAML object.
    media->SetSource(speechStream, speechStream->ContentType);
    media->AutoPlay = true;
    media->Play();
});

此範例示範如何從 SSML 字串產生語音音訊串流,其中包含一些控制語音輸出音調、說話速率和音量的音調元素。

// The string to speak with SSML customizations.
string Ssml =
    @"<speak version='1.0' " +
    "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'>" +
    "Hello <prosody contour='(0%,+80Hz) (10%,+80%) (40%,+80Hz)'>World</prosody> " + 
    "<break time='500ms'/>" +
    "Goodbye <prosody rate='slow' contour='(0%,+20Hz) (10%,+30%) (40%,+10Hz)'>World</prosody>" +
    "</speak>";

// The media object for controlling and playing audio.
MediaElement mediaElement = this.media;

// The object for controlling the speech synthesis engine (voice).
var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

// Generate the audio stream from plain text.
SpeechSynthesisStream stream = await synth.synthesizeSsmlToStreamAsync(Ssml);

// Send the stream to the media object.
mediaElement.SetSource(stream, stream.ContentType);
mediaElement.Play();
// The object for controlling the speech synthesis engine (voice).
synth = ref new SpeechSynthesizer();
// The media object for controlling and playing audio.
media = ref new MediaElement();
// The string to speak.
String^ ssml =
    "<speak version='1.0' "
    "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'>"
    "Hello <prosody contour='(0%,+80Hz) (10%,+80%) (40%,+80Hz)'>World</prosody>"
    "<break time='500ms' /> "
    "Goodbye <prosody rate='slow' contour='(0%,+20Hz) (10%,+30%) (40%,+10Hz)'>World</prosody>"
    "</speak>";

// Generate the audio stream from SSML.
task<SpeechSynthesisStream ^> speakTask = create_task(synth->SynthesizeSsmlToStreamAsync(ssml));
speakTask.then([this, ssml](SpeechSynthesisStream ^speechStream)
{
    // Send the stream to the media object.
    // media === MediaElement XAML object.
    media->SetSource(speechStream, speechStream->ContentType);
    media->AutoPlay = true;
    media->Play();
});

備註

系統只能使用安裝在系統上的 Microsoft 簽署語音來產生語音。

Windows 包含各種 Microsoft 簽署的語音,可用於多種語言。 每個語音都會以單一語言產生合成語音,如特定國家/地區所說。

根據預設,新的 SpeechSynthesizer 物件會使用目前的系統語音 (呼叫 DefaultVoice ,以找出預設語音) 的內容。

若要指定安裝在使用者系統上的任何其他語音合成 (文字轉換語音) 語音,請使用 Voice 方法 (找出系統上已安裝的 語音 ,呼叫 AllVoices) 。

如果您未指定語言,則會載入最接近 [語言] 控制台中所選取語言的語音。

使用 SpeechSynthesizer 物件來:

版本歷程記錄

Windows 版本 SDK 版本 已新增值
1703 15063 選項
1709 16299 TrySetDefaultVoiceAsync

建構函式

SpeechSynthesizer()

初始化 SpeechSynthesizer 物件的新實例。

屬性

AllVoices

取得所有已安裝語音合成引擎的集合, (語音) 。

DefaultVoice

取得預設語音合成引擎 (語音) 。

Options

取得可在 SpeechSynthesizer 物件上設定之選項組合的參考。

Voice

取得或設定語音合成引擎 (語音) 。

方法

Close()

關閉 SpeechSynthesizer 並釋放系統資源。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

SynthesizeSsmlToStreamAsync(String)

以非同步方式從 語音合成標記語言 (SSML) 1.1 版字串產生及控制語音輸出。

SynthesizeTextToStreamAsync(String)

以非同步方式從字串產生語音輸出。

TrySetDefaultVoiceAsync(VoiceInformation)

以非同步方式嘗試在 IoT 裝置上設定用於語音合成的語音。

注意

這個方法僅適用于 內嵌模式

適用於

另請參閱