locked
How do I pause my loop until text-to-speech has read every word in a string?

    Question

  • I am using the speech synthesis control for text to speech.  Instead of importing the entire document in, I'm importing each paragraph into the speech synthesis string.  The this.media.Play() doesn't have an await, so it loop doesn't wait for the speech to finish before going to the next paragraph.

    How do I pause the while loop to wait for this.media.Play() to finish reading the imported string before going to the next paragraph?

    Monday, December 29, 2014 9:05 PM

Answers

  • If you need help please provide full and clear information on what you are doing. A code snippet showing what you're trying to do is helpful as well as a clear explanation of the expected results and how they differ from the actual results. If it takes a large amount of code to reproduce the problem (several files, or more than a screen) then please share a minimal repro on your OneDrive.

    That said, I'll guess that this.media is a MediaElement, in which case you can listen to the MediaElement.CurrentStateChanged property to detect when the media finishes playing.

    Monday, December 29, 2014 9:09 PM
    Owner
  • You won't really be able to do this in a loop properly. You will have to adopt an event driven approach. Basically you would read the first paragraph, register to the MediaElement's CurrentStateChanged Event and then have the MediaElement read it out loud. When the event triggers you would then load the next paragraph and start Playback again. When Playback finished the event would trigger and you would read the next paragraph, etc.

    Basically that is what async/await does behind the scenes anyway in APIs that support it - it just hides those details and makes it easier to write and read asynchronous code.

    One way to perhaps optimize this would be to start playback and immediately load the next paragraph using a ThreadPool thread so it's already loaded and synthesized when the event fires. You would then have it read the next paragraph and again launch your ThreadPool thread to load the next one.

    Tuesday, December 30, 2014 8:38 AM

All replies

  • If you need help please provide full and clear information on what you are doing. A code snippet showing what you're trying to do is helpful as well as a clear explanation of the expected results and how they differ from the actual results. If it takes a large amount of code to reproduce the problem (several files, or more than a screen) then please share a minimal repro on your OneDrive.

    That said, I'll guess that this.media is a MediaElement, in which case you can listen to the MediaElement.CurrentStateChanged property to detect when the media finishes playing.

    Monday, December 29, 2014 9:09 PM
    Owner
  • You won't really be able to do this in a loop properly. You will have to adopt an event driven approach. Basically you would read the first paragraph, register to the MediaElement's CurrentStateChanged Event and then have the MediaElement read it out loud. When the event triggers you would then load the next paragraph and start Playback again. When Playback finished the event would trigger and you would read the next paragraph, etc.

    Basically that is what async/await does behind the scenes anyway in APIs that support it - it just hides those details and makes it easier to write and read asynchronous code.

    One way to perhaps optimize this would be to start playback and immediately load the next paragraph using a ThreadPool thread so it's already loaded and synthesized when the event fires. You would then have it read the next paragraph and again launch your ThreadPool thread to load the next one.

    Tuesday, December 30, 2014 8:38 AM