Question

I'm developing an app where I would like to pause in between when the SpeechSynthesizer.SpeakTextAsync is running and resume back from there.

await synthesizer.SpeakTextAsync(text);

stop reading when var stop = true;

Was it helpful?

Solution

Some one posted here a while back, at the same time I refreshed the page, read his answer, saw a notification & refreshed the page again & the answer was gone. But whoever posted, he is a life saver. It stuck my mind and I ended up creating this.

    String text;  // your text to read out loud
    String[] parts = text.Split(' ');
    int max = parts.Length;
    int count = 0;

    private String makeSSML() {
        if (count == max) { 
            count= 0;
        }
        String s = "<speak version=\"1.0\" ";
        s += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"en-US\">";
        for (int i = count; i < max; i++)
        {
            s += parts[i];
            s += "<mark name=\"anything\"/>";
        }
        s += "<mark name=\"END\"/>";
        s += "</speak>";
        return s;
    }

    private void playIT(){
        synth = new SpeechSynthesizer();
        synth.BookmarkReached += synth_BookmarkReached;
        synth.SpeakSsmlAsync(makeSSML());
    }

    private void synth_BookmarkReached(object sender, SpeechBookmarkReachedEventArgs e)
    {
        count++;
        if (e.Bookmark == "END") {
            synth.Dispose();
        }
    }

    private void Pause_Click(object sender, RoutedEventArgs e)
    {
        synth.Dispose();
    }

Thanks man, your answer gave me the idea.

OTHER TIPS

Well, according to the documentation, when you call CancellAll, you're cancelling the Tasks that are executing asynchronously. By contract, this results in an OperationCancelledException being thrown. That means that wherever you call SpeakTextAsync, SpeakSsmlAsync or SpeakSsmlFromUriAsync, you must surround these calls with a try/catch statement to prevent this exception from going uncaught.

Example:

private static SpeechSynthesizer synth;

public async static Task<SpeechSynthesizer> SpeechSynth(string dataToSpeak)
        {
            synth = new SpeechSynthesizer();
            IEnumerable<VoiceInformation> englishVoices = from voice in InstalledVoices.All
                                                          where voice.Language == "en-US"
                                                          && voice.Gender.Equals(VoiceGender.Female)
                                                          select voice;
            if (englishVoices.Count() > 0)
            {
                synth.SetVoice(englishVoices.ElementAt(0));
            }

            await synth.SpeakTextAsync(dataToSpeak); 

            return synth;
        }  


public static void CancelSpeech()
        {
            synth.CancelAll();
        }

Now call the SpeechSynth("Some Data to Speak") where you want, and whenever you want to cancel it, just call CancelSpeech().

Its Done! Enjoy...!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top