Audio and Video

WPF can incorporate video and audio into a user interface. The relationship between video and animation goes deeper than the superficial fact that they are both ways of showing moving images. The timeline and clock mechanisms you use in animation you can also use to control video and audio playback.

The fact that video or audio can connect with the animation timing system is useful if you want to synchronize animation with media. For example, suppose you wanted to present a recording in an application with a series of clickable bookmarks to jump directly to key points in the recording. You might want to arrange for these bookmarks to change appearance as the playback reaches the relevant point, allowing the user to see at a glance where the playback has reached. One way to do this is to create a timeline that changes each bookmark item at the appropriate time. By connecting this timeline with the media playback, you could guarantee that the visual changes to the bookmarks were always properly synchronized with the video or audio playback.

It's not always necessary to synchronize media and animations. WPF also lets you play media in a simpler "independent" mode. We'll look at this simple usage before seeing how to synchronize playback with animations.

Simple Media Playback

The easiest way to play video or audio media in WPF is to use the MediaElement, pointing its Source property at the media file you wish to play. Example 16-38 shows a MediaElement that will play one ...

Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.