* Updated H2 headings for US-1889327. * Review edits. * Apply suggestions from code review Co-authored-by: Andy (Steve) De George <[email protected]>
2.6 KiB
title, description, ms.date, ms.custom, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | ms.custom | dev_langs | helpviewer_keywords | ms.assetid | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Control a MediaElement (Play, Pause, Stop, Volume, and Speed) | Control playback of media in Windows Presentation foundation (WPF). Start, stop, pause, skip back and forth, and adjust volume and speed. | 03/30/2017 | devdivchpfy22 |
|
|
6885a730-e054-4c16-8c1e-ffe17b1f7c32 |
How to: Control a MediaElement (Play, Pause, Stop, Volume, and Speed)
The following example shows how to control playback of media using a xref:System.Windows.Controls.MediaElement. The example creates a simple media player that allows you to play, pause, stop, and skip back and forth in the media as well as adjust the volume and speed ratio.
Create the UI
The code below creates the UI.
Note
The xref:System.Windows.Controls.MediaElement.LoadedBehavior%2A property of xref:System.Windows.Controls.MediaElement must be set to
Manualin order to be able to interactively stop, pause, and play the media.
[!code-xamlMediaGallery_snip#MediaElementExampleWholePage]
Implement the UI control
The code below implements the functionality of the sample UI controls. The xref:System.Windows.Controls.MediaElement.Play%2A, xref:System.Windows.Controls.MediaElement.Pause%2A, and xref:System.Windows.Controls.MediaElement.Stop%2A methods are used to respectively play, pause and stop the media. Changing the xref:System.Windows.Controls.MediaElement.Position%2A property of the xref:System.Windows.Controls.MediaElement allows you to skip around in the media. Finally, the xref:System.Windows.Controls.MediaElement.Volume%2A and xref:System.Windows.Controls.MediaElement.SpeedRatio%2A properties are used to adjust the volume and playback speed of the media.
[!code-csharpMediaGallery_snip#CodeBehindMediaElementExampleWholePage] [!code-vbMediaGallery_snip#CodeBehindMediaElementExampleWholePage]