581 Tutorial 5

<< Back to the 581 tutorial overview page

Its always great to have some audio output. This tutorial includes sample code that shows you how to play an audio file, and how to synthesise speech.

Playing an audio file

This tutorial shows you how to use the mediaElement object from WPF. In particular, it illustrates how you can play, plause, stop, seek, and adjust the volume of a song.

The code should be self-explanatory, so I won't bother explaining it. Note that the slider implementing seek tries to find a new position as you drag the slider vs. after you let go, so its jittery. This should be fixed so it seeks only after you complete the drag. The slider also uses the selection range to show where in the audio we are (let it play for a bit, and you will see it).

Additional information: Chapter 14 in the Windows Presentation Foundation Unleashed book.

Download: WPFPlaySong.zip project

Note: you can also play sounds through the SoundPlayer class; it is even simpler, but that doesn't have as much power.

Speech synthesis

This tutorial shows you how to use the speech synthesis facilities that can be accessed from WPF. In particular, it illustrates some very basic uses of the SpeechSynthesize and PromptBuilder class objects.

As you type, it will repeat the last sentence just after you type a punctuation mark (.?,). If you click the button, it will say the time, then it will repeat the entire text in a speeded up male voice.

Note that your computer may not have all the 'voices' installed, so if you try to change some voice parameters (e.g., gender) it may have no effect.

Caveats. If you try to generate speech on the fly (e.g., saying a letter as you type it), you will notice significant delays. That is, it takes a while for it to start speaking after you invoke the Speak method.

The code should be self-explanatory, so I won't bother explaining it.

Additional information: Chapter 14 in the Windows Presentation Foundation Unleashed book.

Download: WPFSpeakToMe.zip project