Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/guorg/gu.wpf.media
Wrapper for System.Windows.Controls.MediaElement.
https://github.com/guorg/gu.wpf.media
Last synced: about 4 hours ago
JSON representation
Wrapper for System.Windows.Controls.MediaElement.
- Host: GitHub
- URL: https://github.com/guorg/gu.wpf.media
- Owner: GuOrg
- License: mit
- Created: 2016-09-13T05:46:11.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-25T13:09:59.000Z (11 months ago)
- Last Synced: 2024-03-26T07:08:22.431Z (8 months ago)
- Language: C#
- Homepage:
- Size: 2.5 MB
- Stars: 33
- Watchers: 3
- Forks: 10
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Gu.Wpf.Media
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
[![NuGet](https://img.shields.io/nuget/v/Gu.Wpf.Media.svg)](https://www.nuget.org/packages/Gu.Wpf.Media/)
[![Build status](https://ci.appveyor.com/api/projects/status/eg96glf1osi8on6y/branch/master?svg=true)](https://ci.appveyor.com/project/JohanLarsson/gu-wpf-media/branch/master)
[![Build Status](https://dev.azure.com/guorg/Gu.Wpf.Media/_apis/build/status/GuOrg.Gu.Wpf.Media?branchName=master)](https://dev.azure.com/guorg/Gu.Wpf.Media/_build/latest?definitionId=24&branchName=master)Wrapper for System.Windows.Controls.MediaElement.
# Contents
- [1. MediaElementWrapper](#1-mediaelementwrapper)
- [1.1 Properties](#11-properties)
- [1.1.1 State (`MediaState`)](#111-state-mediastate)
- [1.1.2. Position (`Timespan?`)](#112-position-timespan)
- [1.1.3. Length (`Timespan?`)](#113-length-timespan)
- [1.1.4. CanPauseMedia (`bool?`)](#114-canpausemedia-bool)
- [1.1.5. NaturalVideoHeight (`int?`)](#115-naturalvideoheight-int)
- [1.1.6. NaturalVideoWidth (`int?`)](#116-naturalvideowidth-int)
- [1.1.7. HasAudio (`bool?`)](#117-hasaudio-bool)
- [1.1.8. HasVideo (`bool?`)](#118-hasvideo-bool)
- [1.1.8. HasMedia (`bool`)](#118-hasmedia-bool)
- [1.1.9. SpeedRatio (`double`)](#119-speedratio-double)
- [1.1.10. IsBuffering (`bool`)](#1110-isbuffering-bool)
- [1.1.11. DownloadProgress (`double`)](#1111-downloadprogress-double)
- [1.1.12. BufferingProgress (`double`)](#1112-bufferingprogress-double)
- [1.1.13. VolumeIncrement (`double`)](#1113-volumeincrement-double)
- [1.1.14. VideoFormats](#1114-videoformats)
- [1.1.15. AudioFormats](#1115-audioformats)
- [1.1.16. Source (`Uri`)](#1116-source-uri)
- [1.1.17. Volume (`double`)](#1117-volume-double)
- [1.1.18. Balance (`double`)](#1118-balance-double)
- [1.1.19. IsMuted (`bool`)](#1119-ismuted-bool)
- [1.1.20. ScrubbingEnabled (`bool`)](#1120-scrubbingenabled-bool)
- [1.1.21. Stretch (`Stretch`)](#1121-stretch-stretch)
- [1.1.22. StretchDirection (`StretchDirection`)](#1122-stretchdirection-stretchdirection)
- [1.1.22. LoadedBehavior (`MediaState`)](#1122-loadedbehavior-mediastate)
- [1.2. Events](#12-events)
- [1.2.1. MediaFailed](#121-mediafailed)
- [1.2.2. MediaOpened](#122-mediaopened)
- [1.2.3. BufferingStarted](#123-bufferingstarted)
- [1.2.4. BufferingEnded](#124-bufferingended)
- [1.2.5. ScriptCommand](#125-scriptcommand)
- [1.2.6. MediaEnded](#126-mediaended)
- [1.3. CommandBindings](#13-commandbindings)
- [1.3.1 IncreaseVolume & DecreaseVolume](#131-increasevolume--decreasevolume)
- [1.3.2 Skip, SkipForward & SkipBack](#132-skip-skipforward--skipback)
- [2. MouseWheelGesture](#2-mousewheelgesture)
- [3. Icon](#3-icon)
- [4. Drag](#4-drag)
- [5. TimeSpanToStringConverter](#5-timespantostringconverter)
- [6. Commands](#6-commands)
- [7. Sample](#7-sample)# 1. MediaElementWrapper
## 1.1 Properties
The wrapper wraps the properties of System.Windows.Controls.MediaElement and adds a couple of new properties.
Mapped properties are dependency properties that are updated when needed.### 1.1.1 State (`MediaState`)
The current `MediaState` of the player.### 1.1.2. Position (`Timespan?`)
The current position in the media, `null` if no media is loaded.
Twoway bindable and updates every 0.1 s when playing.### 1.1.3. Length (`Timespan?`)
The length of the current media, `null`if no media is loaded.### 1.1.4. CanPauseMedia (`bool?`)
Mapped to System.Windows.Controls.MediaElement.CanPause, `null`if no media is loaded.### 1.1.5. NaturalVideoHeight (`int?`)
Mapped to System.Windows.Controls.MediaElement.NaturalVideoHeight, `null`if no media is loaded.### 1.1.6. NaturalVideoWidth (`int?`)
Mapped to System.Windows.Controls.MediaElement.NaturalVideoWidth, `null`if no media is loaded.### 1.1.7. HasAudio (`bool?`)
Mapped to System.Windows.Controls.MediaElement.HasAudio, `null`if no media is loaded.### 1.1.8. HasVideo (`bool?`)
Mapped to System.Windows.Controls.MediaElement.HasVideo, `null`if no media is loaded.### 1.1.8. HasMedia (`bool`)
Returns true if media is loaded.### 1.1.9. SpeedRatio (`double`)
Mapped to System.Windows.Controls.MediaElement.SpeedRatio.### 1.1.10. IsBuffering (`bool`)
Mapped to System.Windows.Controls.MediaElement.IsBuffering.### 1.1.11. DownloadProgress (`double`)
Mapped to System.Windows.Controls.MediaElement.DownloadProgress.
Updated every 1 s when buffering.### 1.1.12. BufferingProgress (`double`)
Mapped to System.Windows.Controls.MediaElement.BufferingProgress.
Updated every 1 s when buffering.### 1.1.13. VolumeIncrement (`double`)
How much volume is changed when MediaCommands.IncreaseVolume & MediaCommands.DecreaseVolume are invoked.
Default 0.05;### 1.1.14. VideoFormats
A list of video file formats for convenience.
*.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf; *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webmUsage:
```c#
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = $"Media files|{this.MediaElement.VideoFormats}|All files (*.*)|*.*"
};if (openFileDialog.ShowDialog() == true)
{
this.MediaElement.Source = new Uri(openFileDialog.FileName);
}
```### 1.1.15. AudioFormats
A list of audio file formats for convenience.
*.mp3; *.wma; *.aac; *.adt; *.adts; *.m4a; *.wav; *.aif; *.aifc; *.aiff; *.cdaUsage:
```c#
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = $"Media files|{this.MediaElement.AudioFormats}|All files (*.*)|*.*"
};if (openFileDialog.ShowDialog() == true)
{
this.MediaElement.Source = new Uri(openFileDialog.FileName);
}
```### 1.1.16. Source (`Uri`)
Mapped to System.Windows.Controls.MediaElement.Source.
When source changes play is invoked to trigger load. Then pause is invoked in the MediaOpened event.
This results in the video paused at the first frame as initial state after setting `Source`
Subscribe to `MediaOpened` if you want to start playing on load.### 1.1.17. Volume (`double`)
Mapped to System.Windows.Controls.MediaElement.Volume.### 1.1.18. Balance (`double`)
Mapped to System.Windows.Controls.MediaElement.Balance.### 1.1.19. IsMuted (`bool`)
Mapped to System.Windows.Controls.MediaElement.IsMuted.### 1.1.20. ScrubbingEnabled (`bool`)
Mapped to System.Windows.Controls.MediaElement.ScrubbingEnabled.### 1.1.21. Stretch (`Stretch`)
Mapped to System.Windows.Controls.MediaElement.Stretch.### 1.1.22. StretchDirection (`StretchDirection`)
Mapped to System.Windows.Controls.MediaElement.StretchDirection.### 1.1.22. LoadedBehavior (`MediaState`)
Set initial state after media is loaded. This is very unlike MediaElement.LoadedBehavior where setting it to paused will throw if touching play.## 1.2. Events
### 1.2.1. MediaFailed
Mapped to System.Windows.Controls.MediaElement.MediaFailed.### 1.2.2. MediaOpened
Mapped to System.Windows.Controls.MediaElement.MediaOpened.### 1.2.3. BufferingStarted
Mapped to System.Windows.Controls.MediaElement.BufferingStarted.### 1.2.4. BufferingEnded
Mapped to System.Windows.Controls.MediaElement.BufferingEnded.### 1.2.5. ScriptCommand
Mapped to System.Windows.Controls.MediaElement.ScriptCommand.### 1.2.6. MediaEnded
Mapped to System.Windows.Controls.MediaElement.MediaEnded.## 1.3. CommandBindings
Command bindings for:
- MediaCommands.Play
- MediaCommands.Pause
- MediaCommands.Stop
- MediaCommands.TogglePlayPause
- MediaCommands.Rewind
- MediaCommands.IncreaseVolume
- MediaCommands.DecreaseVolume
- MediaCommands.MuteVolume
- Commands.ToggleMute
- Commands.UnmuteVolume
- Commands.Skip
- Commands.SkipForward
- Commands.SkipBack### 1.3.1 IncreaseVolume & DecreaseVolume
If parameter is null the value of VolumeIncrement is used.### 1.3.2 Skip, SkipForward & SkipBack
If parameter is `null` the value of SkipIncrement is used.
If parameter is an `int` the value of parameter*SkipIncrement is used.
If parameter is `double` `TimeSpan.FromSeconds(parameter) is used.
If parameter is TimeSpan it is used.
Values can be negative.# 2. MouseWheelGesture
For using mousewheel in inputbindings
```xaml
```
And with modifier
```xaml
```
# 3. Icon
Exposes a `Geometry` attached property.
```xaml```
# 4. Drag
Exposes a `PauseWhileDragging` attached property.
When binidng this to a `MediaElementWrapper`playback is paused while dragging.```xaml
```
# 5. TimeSpanToStringConverter
Converts Timespans like this:|Time|Result|
|---|---|
|null|-:--|
|00:00:01|0:01|
|00:00:12|0:12|
|00:01:23|1:23|
|00:12:34|12:23|
|01:23:45|1:23:45|# 6. Commands
- Commands.ToggleMute
- Commands.UnmuteVolume
- Commands.ToggleFullScreen
- Commands.BeginFullScreen
- Commands.EndFullScreen
- Commands.Skip
- Commands.SkipForward
- Commands.SkipBackMediaElementWrapper has a command bindings for:
- Commands.ToggleMute
- Commands.UnmuteVolume
- Commands.Skip
- Commands.SkipForward
- Commands.SkipBack# 7. Sample
```xaml
<Setter Property="media:Icon.Geometry" Value="{StaticResource {x:Static media:Geometries.PauseGeometryKey}}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="media:Icon.Geometry" Value="{StaticResource {x:Static media:Geometries.PlayGeometryKey}}" />
</Trigger>
<Setter Property="media:Icon.Geometry" Value="{StaticResource {x:Static media:Geometries.UnMuteGeometryKey}}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="media:Icon.Geometry" Value="{StaticResource {x:Static media:Geometries.MuteGeometryKey}}" />
</Trigger>
```
With code behind:
```c#
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32;public partial class MainWindow : Window
{
private Stretch stretch;public MainWindow()
{
this.InitializeComponent();
}private void OpenExecuted(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = $"Media files|{this.MediaElement.VideoFormats}|All files (*.*)|*.*"
};if (openFileDialog.ShowDialog() == true)
{
this.MediaElement.SetCurrentValue(MediaElementWrapper.SourceProperty, new Uri(openFileDialog.FileName));
}
}private void OnToggleFullScreenExecuted(object sender, ExecutedRoutedEventArgs e)
{
if (this.WindowStyle == WindowStyle.SingleBorderWindow)
{
this.stretch = this.MediaElement.Stretch;
this.MediaElement.Stretch = Stretch.Uniform;
this.WindowStyle = WindowStyle.None;
this.SizeToContent = SizeToContent.Manual;
this.WindowState = WindowState.Maximized;
}
else
{
this.MediaElement.Stretch = this.stretch;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.SizeToContent = SizeToContent.WidthAndHeight;
this.WindowState = WindowState.Normal;
}e.Handled = true;
}private void OnEndFullScreenCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.WindowState == WindowState.Maximized && this.WindowStyle == WindowStyle.None;
}private void OnEndFullScreenExecuted(object sender, ExecutedRoutedEventArgs e)
{
this.MediaElement.Stretch = this.stretch;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.SizeToContent = SizeToContent.WidthAndHeight;
this.WindowState = WindowState.Normal;
e.Handled = true;
}
}
```![image](https://cloud.githubusercontent.com/assets/1640096/18583461/fa5dd2e8-7c0a-11e6-988c-e4a1bc692039.png)
Check out the demo project for more samples.
Sample video from https://pixabay.com/