Share via


SpeechSynthesizer Klasse

Definition

Bietet Zugriff auf die Funktionalität einer installierten Sprachsynthese-Engine (Voice) für TTS-Dienste (Text-to-Speech).

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
Vererbung
Object Platform::Object IInspectable SpeechSynthesizer
Attribute
Implementiert

Windows-Anforderungen

Gerätefamilie
Windows 10 (eingeführt in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (eingeführt in v1.0)

Beispiele

Das folgende Beispiel zeigt, wie Sie einen Sprachaudiodatenstrom aus einer einfachen Textzeichenfolge generieren.

// 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();
});

In diesem Beispiel wird gezeigt, wie Sie einen Sprachaudiodatenstrom aus einer SSML-Zeichenfolge generieren, der einige Modulationselemente enthält, die die Tonhöhe, Sprechrate und Lautstärke der Sprachausgabe steuern.

// 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();
});

Hinweise

Nur von Microsoft signierte Stimmen, die auf dem System installiert sind, können zum Generieren von Sprache verwendet werden.

Windows enthält verschiedene von Microsoft signierte Stimmen, die für eine Reihe von Sprachen verwendet werden können. Jede Stimme generiert synthetisierte Sprache in einer einzelnen Sprache, die in einem bestimmten Land/einer bestimmten Region gesprochen wird.

Standardmäßig verwendet ein neues SpeechSynthesizer-Objekt die aktuelle Systemstimme (rufen Sie DefaultVoice auf, um herauszufinden, was die Standardstimme ist).

Verwenden Sie die Voice-Methode ( um herauszufinden, welche Stimmen auf dem System des Benutzers installiert sind) (um herauszufinden, welche Stimmen auf dem System installiert sind, rufen Sie AllVoices auf).

Wenn Sie keine Sprache angeben, wird die Stimme geladen, die der in der Sprachsteuerung ausgewählten Sprache am ehesten entspricht.

Verwenden Sie ein SpeechSynthesizer-Objekt für Folgendes:

Versionsverlauf

Windows-Version SDK-Version Mehrwert
1703 15063 Optionen
1709 16299 TrySetDefaultVoiceAsync

Konstruktoren

SpeechSynthesizer()

Initialisiert eine neue instance eines SpeechSynthesizer-Objekts.

Eigenschaften

AllVoices

Ruft eine Auflistung aller installierten Sprachsynthese-Engines (Stimmen) ab.

DefaultVoice

Ruft die Standardmäßige Sprachsynthese-Engine (Voice) ab.

Options

Ruft einen Verweis auf die Auflistung von Optionen ab, die für das SpeechSynthesizer-Objekt festgelegt werden können.

Voice

Ruft die Sprachsynthese-Engine (Stimme) ab oder legt sie fest.

Methoden

Close()

Schließt speechSynthesizer und gibt Systemressourcen frei.

Dispose()

Führt anwendungsspezifische Aufgaben durch, die mit der Freigabe, der Zurückgabe oder dem Zurücksetzen von nicht verwalteten Ressourcen zusammenhängen.

SynthesizeSsmlToStreamAsync(String)

Generieren und steuern Sie die Sprachausgabe asynchron aus einer SSML-Zeichenfolge (Speech Synthesis Markup Language, Version 1.1).

SynthesizeTextToStreamAsync(String)

Generieren Sie die Sprachausgabe asynchron aus einer Zeichenfolge.

TrySetDefaultVoiceAsync(VoiceInformation)

Versucht asynchron, die für die Sprachsynthese auf einem IoT-Gerät verwendete Stimme festzulegen.

Hinweis

Diese Methode ist nur im eingebetteten Modus verfügbar.

Gilt für:

Weitere Informationen