다음을 통해 공유


Making your app speak

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Adding speech to your app takes only a few lines of code. Can you say "Easy peasy?"

Making your app talk is easy with Windows 8.1, Microsoft Visual Studio 2013, and the SpeechSynthesis namespace.

Adding speech to a C# app

Note   Speech synthesis is only supported in Windows 8.1. Using this code on Windows Phone 8.1 causes an exception to be thrown.

using Windows.Media.SpeechSynthesis; 
using System.Threading.Tasks;
private async Task SayMethod()
{
MediaElement mediaElement = new MediaElement(); 
       var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
       SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Hello World! Windows can talk!");
       mediaElement.SetSource(stream, stream.ContentType);
       mediaElement.Play();
       return;
}

Adding speech to a JavaScript app

    function talk() {
        var audio = new Audio();
        var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();
        synth.synthesizeTextToStreamAsync("Hello world! Windows can talk!").then(function (synthesisStream) {
            var blob = MSApp.createBlobFromRandomAccessStream(synthesisStream.contentType, synthesisStream);
            audio.src = URL.createObjectURL(blob, { oneTimeOnly: true });
            audio.play();
        })
    };

Adding speech to a C++ app

using namespace Windows::Media::SpeechSynthesis;
using namespace Concurrency;

SpeechSynthesizer^ synth;
MediaElement ^media;

void TalkTalkCPP::MainPage::Talk()
{
    synth = ref new SpeechSynthesizer();
    media = ref new MediaElement();
    String^ text = "Hello World!Windows can talk!";
    task<SpeechSynthesisStream ^> speakTask = create_task(synth->SynthesizeTextToStreamAsync(text));
    speakTask.then([this, text](SpeechSynthesisStream ^speechStream)
    {
        media->SetSource(speechStream, speechStream->ContentType);
        media->AutoPlay = true;
        media->Play();
    });
}

Speech synthesis sample

SpeechSynthesis