SpeechSynthesizer Kelas

Definisi

Menyediakan akses ke fungsionalitas mesin sintesis ucapan (suara) yang diinstal untuk layanan Teks ke ucapan (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
Warisan
Object Platform::Object IInspectable SpeechSynthesizer
Atribut
Penerapan

Persyaratan Windows

Rangkaian perangkat
Windows 10 (diperkenalkan dalam 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (diperkenalkan dalam v1.0)

Contoh

Contoh berikut menunjukkan cara membuat aliran audio ucapan dari string teks dasar.

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

Contoh ini menunjukkan cara menghasilkan aliran audio ucapan dari string SSML, yang mencakup beberapa elemen modulasi yang mengontrol nada, laju bicara, dan volume output ucapan.

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

Keterangan

Hanya suara yang ditandatangani Microsoft yang diinstal pada sistem yang dapat digunakan untuk menghasilkan ucapan.

Windows menyertakan berbagai suara bertanda tangan Microsoft yang dapat digunakan untuk sejumlah bahasa. Setiap suara menghasilkan ucapan yang disintesis dalam satu bahasa, seperti yang diucapkan di negara/wilayah tertentu.

Secara default, objek SpeechSynthesizer baru menggunakan suara sistem saat ini (panggil DefaultVoice untuk mengetahui apa suara defaultnya).

Untuk menentukan suara sintesis ucapan lainnya (teks ke ucapan) yang diinstal pada sistem pengguna, gunakan metode Voice (untuk mengetahui suara mana yang diinstal pada sistem, panggil AllVoices).

Jika Anda tidak menentukan bahasa, suara yang paling sesuai dengan bahasa yang dipilih di panel Kontrol bahasa dimuat.

Gunakan objek SpeechSynthesizer untuk:

Riwayat versi

Versi Windows Versi SDK Nilai ditambahkan
1703 15063 Opsi
1709 16299 TrySetDefaultVoiceAsync

Konstruktor

SpeechSynthesizer()

Menginisialisasi instans baru objek SpeechSynthesizer .

Properti

AllVoices

Mendapatkan koleksi semua mesin sintesis ucapan (suara) yang diinstal.

DefaultVoice

Mendapatkan mesin sintesis ucapan default (suara).

Options

Mendapatkan referensi ke kumpulan opsi yang dapat diatur pada objek SpeechSynthesizer.

Voice

Mendapatkan atau mengatur mesin sintesis ucapan (suara).

Metode

Close()

Menutup SpeechSynthesizer dan merilis sumber daya sistem.

Dispose()

Melakukan tugas yang ditentukan aplikasi yang terkait dengan membebaskan, merilis, atau mengatur ulang sumber daya yang tidak dikelola.

SynthesizeSsmlToStreamAsync(String)

Hasilkan dan kontrol output ucapan secara asinkron dari string Speech Synthesis Markup Language (SSML) Versi 1.1 .

SynthesizeTextToStreamAsync(String)

Secara asinkron menghasilkan output ucapan dari string.

TrySetDefaultVoiceAsync(VoiceInformation)

Secara asinkron mencoba mengatur suara yang digunakan untuk sintesis ucapan pada perangkat IoT .

Catatan

Metode ini hanya tersedia dalam mode Tersemat.

Berlaku untuk

Lihat juga