--- title: "Multimedia Overview" ms.date: "03/30/2017" helpviewer_keywords: - "multimedia [WPF]" - "media [WPF]" ms.assetid: feb25b15-d741-4ac3-818f-1b19f63a3562 --- # Multimedia Overview The multimedia features in [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] enable you to integrate audio and video into your applications to enhance the user experience. This topic introduces the multimedia features of [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)]. ## Media API The and 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. is a that is supported by the [Layout](../advanced/layout.md) and can be consumed as the content of many controls. It is also usable in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] as well as code. , on the other hand, is designed for objects and lacks layout support. Media loaded using a can only be presented using a or by directly interacting with a . cannot be used in XAML. For more information about drawing objects and drawing context, see [Drawing Objects Overview](drawing-objects-overview.md). > [!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 and have similar members. The links in this section refer to the class members. Unless specifically noted, members linked to in the class can also be found in the class. To understand media playback in [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)], an understanding of the different modes in which media can be played is required. Both and can be used in two different media modes, independent mode and clock mode. The media mode is determined by the property. When is `null`, the media object is in independent mode. When the 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 can be directly specified. - Media playback can be directly controlled. - Media's and properties can be modified. Media is loaded by either setting the object's property or by calling the object's method. To control media playback in independent mode, the media object's control methods can be used. The control methods available are , , , and . For , interactive control using these methods is only available when the is set to . These methods are unavailable when the media object is in clock mode. See [Control a MediaElement (Play, Pause, Stop, Volume, and Speed)](how-to-control-a-mediaelement-play-pause-stop-volume-and-speed.md) for an example of independent mode. ### Clock Mode In clock mode, a drives media playback. Clock mode has the following characteristics: - Media's is indirectly set through a . - Media playback can be controlled by the clock. The media object's control methods cannot be used. - Media is loaded by setting a object's property, creating the clock from the timeline, and assigning the clock to the media object. Media is also loaded this way when a inside a targets a . To control media playback in clock mode, the control methods must be used. A is obtained from the property of the . If you attempt to use the control methods of either a or object while in clock mode, an will be thrown. See the [Animation Overview](animation-overview.md) for more information about clocks and timelines. See [Control a MediaElement by Using a Storyboard](how-to-control-a-mediaelement-by-using-a-storyboard.md) for an example of clock mode. ## MediaElement Class Adding media to an application is as simple as adding a control to the [!INCLUDE[TLA#tla_ui](../../../includes/tlasharptla-ui-md.md)] of the application and providing a to the media you wish to include. All media types supported by Microsoft Windows Media Player 10 are supported in [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)]. The following example shows a simple usage of the in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)]. [!code-xaml[MediaElement_snip#SimpleMediaElementUsageWholePage](~/samples/snippets/csharp/VS_Snippets_Wpf/MediaElement_snip/CSharp/SimpleUsage.xaml#simplemediaelementusagewholepage)] 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 object and is controlled by the and properties. ### Controlling a MediaElement The and properties control the behavior of the when is `true` or `false`, respectively. The the properties are set to affect the media playback behavior. For example, the default is and the default is . This means that as soon as the 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 and properties are not the only way to control media playback. In clock mode, the clock can control the and the interactive control methods have control when the is . handles this competition for control by evaluating the following priorities. 1. . In place when media is unloaded. This ensures that all media resources are released by default, even when a is associated with the . 2. . In place when media has a . If media is unloaded, the will take effect as long as the is . Clock mode always overrides the loaded behavior of the . 3. . In place when media is loaded. 4. Interactive control methods. In place when is . The control methods available are , , , and . ### Displaying a MediaElement To display a it must have content to render and it will have its and properties set to zero until content is loaded. For audio only content, these properties are always zero. For video content, once the event has been raised the and will report the size of the loaded media. This means that until media is loaded, the will not take up any physical space in the [!INCLUDE[TLA#tla_ui](../../../includes/tlasharptla-ui-md.md)] unless the or properties are set. Setting both the and properties will cause the media to stretch to fill the area provided for the . To preserve the media's original aspect ratio, either the or property should be set but not both. Setting both the and 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, [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] can preroll the media. This is done by setting the to either or . In a state, the media will preroll and will present the first frame. In a state, the media will preroll and begin to play. ## MediaPlayer Class Where as the class is a framework element, the class is designed to be used in objects. Drawing objects are used when you can sacrifice framework level features to gain performance benefits or when you need features. enables you to take advantage of these features while providing media content in your applications. Like , can be used in independent or clock mode but does not have the object's unloaded and loaded states. This reduces the playback control complexity of the . ### Controlling MediaPlayer Because is stateless, there are only two ways to control media playback. 1. Interactive control methods. In place when in independent mode (`null` property). 2. . In place when media has a . ### Displaying a MediaPlayer Technically, a cannot be displayed since it has no physical representation. However, it can be used to present media in a using the class. The following example demonstrates the use of a to display media. [!code-csharp[DrawingMiscSnippets_snip#VideoDrawingExampleInline](~/samples/snippets/csharp/VS_Snippets_Wpf/DrawingMiscSnippets_snip/CSharp/VideoDrawingExample.cs#videodrawingexampleinline)] See the [Drawing Objects Overview](drawing-objects-overview.md) for more information about objects. ## See also - - [Layout](../advanced/layout.md) - [How-to Topics](audio-and-video-how-to-topics.md)