forked from videolan/libvlcsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewModel.cs
81 lines (72 loc) · 2.32 KB
/
MainViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.ComponentModel;
using System.Windows.Input;
using LibVLCSharp.Platforms.Windows;
using LibVLCSharp.Shared;
namespace LibVLCSharp.UWP.Sample
{
/// <summary>
/// Main view model
/// </summary>
public class MainViewModel : INotifyPropertyChanged, IDisposable
{
/// <summary>
/// Occurs when a property value changes
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Initialized a new instance of <see cref="MainViewModel"/> class
/// </summary>
public MainViewModel()
{
InitializedCommand = new RelayCommand<InitializedEventArgs>(Initialize);
}
/// <summary>
/// Destructor
/// </summary>
~MainViewModel()
{
Dispose();
}
/// <summary>
/// Gets the command for the initialization
/// </summary>
public ICommand InitializedCommand { get; }
private LibVLC LibVLC { get; set; }
private MediaPlayer _mediaPlayer;
/// <summary>
/// Gets the media player
/// </summary>
public MediaPlayer MediaPlayer
{
get => _mediaPlayer;
private set => Set(nameof(MediaPlayer), ref _mediaPlayer, value);
}
private void Set<T>(string propertyName, ref T field, T value)
{
if (field == null && value != null || field != null && !field.Equals(value))
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Initialize(InitializedEventArgs eventArgs)
{
LibVLC = new LibVLC(enableDebugLogs: true, eventArgs.SwapChainOptions);
MediaPlayer = new MediaPlayer(LibVLC);
using var media = new Media(LibVLC, new Uri("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"));
MediaPlayer.Play(media);
}
/// <summary>
/// Cleaning
/// </summary>
public void Dispose()
{
var mediaPlayer = MediaPlayer;
MediaPlayer = null;
mediaPlayer?.Dispose();
LibVLC?.Dispose();
LibVLC = null;
}
}
}