Multimedia Overview
The multimedia features in Windows Presentation Foundation (WPF) enable you to integrate audio and video into your applications to enhance the user experience. This topic introduces the multimedia features of WPF.
Media API
The MediaElement and MediaPlayer classes are used to present audio or video content. These classes can be controlled interactively or by a clock. These classes can use on the Microsoft Windows Media Player 10 control for media playback. Which class you use, depends on the scenario.
MediaElement is a UIElement that is supported by the Layout and can be consumed as the content of many controls. It is also usable in Extensible Application Markup Language (XAML) as well as code. MediaPlayer, on the other hand, is designed for Drawing objects and lacks layout support. Media loaded using a MediaPlayer can only be presented using a VideoDrawing or by directly interacting with a DrawingContext. MediaPlayer cannot be used in XAML.
For more information about drawing objects and drawing context, see Drawing Objects Overview.
Note
When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content
and set CopyToOutputDirectory
to PreserveNewest
or Always
.
Media Playback Modes
Note
Both MediaElement and MediaPlayer have similar members. The links in this section refer to the MediaElement class members. Unless specifically noted, members linked to in the MediaElement class can also be found in the MediaPlayer class.
To understand media playback in Windows Presentation Foundation (WPF), an understanding of the different modes in which media can be played is required. Both MediaElement and MediaPlayer can be used in two different media modes, independent mode and clock mode. The media mode is determined by the Clock property. When Clock is null
, the media object is in independent mode. When the Clock is non-null, the media object is in clock mode. By default, media objects are in independent mode.
Independent Mode
In independent mode, the media content drives media playback. Independent mode enables the following options:
Media's Uri can be directly specified.
Media playback can be directly controlled.
Media's Position and SpeedRatio properties can be modified.
Media is loaded by either setting the MediaElement object's Source property or by calling the MediaPlayer object's Open method.
To control media playback in independent mode, the media object's control methods can be used. The control methods available are Play, Pause, Close, and Stop. For MediaElement, interactive control using these methods is only available when the LoadedBehavior is set to Manual. These methods are unavailable when the media object is in clock mode.
See Control a MediaElement (Play, Pause, Stop, Volume, and Speed) for an example of independent mode.
Clock Mode
In clock mode, a MediaTimeline drives media playback. Clock mode has the following characteristics:
Media's Uri is indirectly set through a MediaTimeline.
Media playback can be controlled by the clock. The media object's control methods cannot be used.
Media is loaded by setting a MediaTimeline object's Source property, creating the clock from the timeline, and assigning the clock to the media object. Media is also loaded this way when a MediaTimeline inside a Storyboard targets a MediaElement.
To control media playback in clock mode, the ClockController control methods must be used. A ClockController is obtained from the ClockController property of the MediaClock. If you attempt to use the control methods of either a MediaElement or MediaPlayer object while in clock mode, an InvalidOperationException will be thrown.
See the Animation Overview for more information about clocks and timelines.
See Control a MediaElement by Using a Storyboard for an example of clock mode.
MediaElement Class
Adding media to an application is as simple as adding a MediaElement control to the user interface (UI) of the application and providing a Uri to the media you wish to include. All media types supported by Microsoft Windows Media Player 10 are supported in Windows Presentation Foundation (WPF). The following example shows a simple usage of the MediaElement in Extensible Application Markup Language (XAML).
<!-- This page shows a simple usage of MediaElement -->
<Page x:Class="MediaElementExample.SimpleUsage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SimpleUsage"
>
<StackPanel Margin="20">
<MediaElement Source="media/numbers-aud.wmv" />
</StackPanel>
</Page>
In this sample, media is played automatically as soon as it is loaded. Once the media has finished playing, the media is closed and all media resources are release (including video memory). This is the default behavior of the MediaElement object and is controlled by the LoadedBehavior and UnloadedBehavior properties.
Controlling a MediaElement
The LoadedBehavior and UnloadedBehavior properties control the behavior of the MediaElement when IsLoaded is true
or false
, respectively. The MediaState the properties are set to affect the media playback behavior. For example, the default LoadedBehavior is Play and the default UnloadedBehavior is Close. This means that as soon as the MediaElement is loaded and the preroll is complete, the media begins to play. Once playback is complete, media is closed and all media resources are released.
The LoadedBehavior and UnloadedBehavior properties are not the only way to control media playback. In clock mode, the clock can control the MediaElement and the interactive control methods have control when the LoadedBehavior is Manual. MediaElement handles this competition for control by evaluating the following priorities.
UnloadedBehavior. In place when media is unloaded. This ensures that all media resources are released by default, even when a MediaClock is associated with the MediaElement.
MediaClock. In place when media has a Clock. If media is unloaded, the MediaClock will take effect as long as the UnloadedBehavior is Manual. Clock mode always overrides the loaded behavior of the MediaElement.
LoadedBehavior. In place when media is loaded.
Interactive control methods. In place when LoadedBehavior is Manual. The control methods available are Play, Pause, Close, and Stop.
Displaying a MediaElement
To display a MediaElement it must have content to render and it will have its ActualWidth and ActualHeight properties set to zero until content is loaded. For audio only content, these properties are always zero. For video content, once the MediaOpened event has been raised the ActualWidth and ActualHeight will report the size of the loaded media. This means that until media is loaded, the MediaElement will not take up any physical space in the user interface (UI) unless the Width or Height properties are set.
Setting both the Width and Height properties will cause the media to stretch to fill the area provided for the MediaElement. To preserve the media's original aspect ratio, either the Width or Height property should be set but not both. Setting both the Width and Height properties will cause the media to present in a fixed element size that may not be desirable.
To avoid having a fixed size element which, Windows Presentation Foundation (WPF) can preroll the media. This is done by setting the LoadedBehavior to either Play or Pause. In a Pause state, the media will preroll and will present the first frame. In a Play state, the media will preroll and begin to play.
MediaPlayer Class
Where as the MediaElement class is a framework element, the MediaPlayer class is designed to be used in Drawing objects. Drawing objects are used when you can sacrifice framework level features to gain performance benefits or when you need Freezable features. MediaPlayer enables you to take advantage of these features while providing media content in your applications. Like MediaElement, MediaPlayer can be used in independent or clock mode but does not have the MediaElement object's unloaded and loaded states. This reduces the playback control complexity of the MediaPlayer.
Controlling MediaPlayer
Because MediaPlayer is stateless, there are only two ways to control media playback.
Interactive control methods. In place when in independent mode (
null
Clock property).MediaClock. In place when media has a Clock.
Displaying a MediaPlayer
Technically, a MediaPlayer cannot be displayed since it has no physical representation. However, it can be used to present media in a Drawing using the VideoDrawing class. The following example demonstrates the use of a VideoDrawing to display media.
//
// Create a VideoDrawing.
//
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));
VideoDrawing aVideoDrawing = new VideoDrawing();
aVideoDrawing.Rect = new Rect(0, 0, 100, 100);
aVideoDrawing.Player = player;
// Play the video once.
player.Play();
See the Drawing Objects Overview for more information about Drawing objects.
See also
.NET Desktop feedback