Beispiel #1
0
        public static async Task SSML2AudioFile(string fileName)
        {
            // The object for controlling the speech synthesis engine (voice).
            var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

            var path     = new FileInfo(Environment.GetCommandLineArgs()[0]).DirectoryName;
            var SsmlText = File.ReadAllText(path + "/" + $"{fileName}.xml");

            // Generate the audio stream from the SSML text.
            using (SpeechSynthesisStream stream = await synth.SynthesizeSsmlToStreamAsync(SsmlText))
            {
                // Get the app's local folder.

                var           user        = (await Windows.System.User.FindAllAsync())[0];
                StorageFolder localFolder = await StorageFolder.GetFolderFromPathForUserAsync(user, path);

                // Send the stream to the audio file.
                using (var reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);

                    var sampleFile = await localFolder.CreateFileAsync($"{fileName}.wav", CreationCollisionOption.ReplaceExisting);

                    await FileIO.WriteBufferAsync(sampleFile, reader.ReadBuffer((uint)stream.Size));
                }
            }
        }
Beispiel #2
0
        private async void readText(string mytext)
        {
            MediaElement mediaplayer = new MediaElement();

            using (var speech = new Windows.Media.SpeechSynthesis.SpeechSynthesizer())
            {
                speech.Voice = SpeechSynthesizer.AllVoices.First(gender => gender.Gender == VoiceGender.Female);
                string ssml = @"<speak version='1.0' " + "xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-UK'>" + mytext + "</speak>";
                SpeechSynthesisStream stream = await speech.SynthesizeSsmlToStreamAsync(ssml);

                mediaplayer.SetSource(stream, stream.ContentType);
                mediaplayer.Play();
            }
        }