Hello,
Welcome to our Microsoft Q&A platform!
Since calling the SynthesizeTextToStreamAsync() method will start an asynchronous operation, in C++/Winrt you need to write a co_await statement to cooperatively await the result of any function that returns the asynchronous operation. Besides, if the asynchronous method does not communicate a result object or ongoing progress, you need to use IAsyncAction to be the return type. For more information about how to use asynchronous operation in C++/Winrt, you can refer to Concurrency and asynchronous operations with C++/WinRT.
The correct code will be the following:
.h
private:
Windows::Foundation::IAsyncAction playsound();
.cpp
Windows::Foundation::IAsyncAction MainPage::playsound() {
synth = Windows::Media::SpeechSynthesis::SpeechSynthesizer();
media = Windows::UI::Xaml::Controls::MediaElement();
// The string to speak.
hstring text = L"Hello World";
Windows::Media::SpeechSynthesis::SpeechSynthesisStream speechStream = co_await synth.SynthesizeTextToStreamAsync(text);
media.SetSource(speechStream, speechStream.ContentType());
media.AutoPlay(true);
media.Play();
}
Besides, for more information about how to use the C++/Winrt in UWP, please check the following article:
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/.
Thanks