-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathMauiMediaElement.windows.cs
More file actions
228 lines (206 loc) · 6.95 KB
/
MauiMediaElement.windows.cs
File metadata and controls
228 lines (206 loc) · 6.95 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Reflection;
using System.Runtime.InteropServices;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Primitives;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Devices;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Markup;
using Application = Microsoft.Maui.Controls.Application;
using Grid = Microsoft.UI.Xaml.Controls.Grid;
using Page = Microsoft.Maui.Controls.Page;
namespace CommunityToolkit.Maui.Core.Views;
/// <summary>
/// The user-interface element that represents the <see cref="MediaElement"/> on Windows.
/// </summary>
public partial class MauiMediaElement : Grid, IDisposable
{
[LibraryImport("user32.dll")]
internal static partial IntPtr GetForegroundWindow();
/// <summary>
/// Safely gets the foreground window handle, returning null if no foreground window exists.
/// </summary>
internal static IntPtr? TryGetForegroundWindow()
{
var hwnd = GetForegroundWindow();
return hwnd == IntPtr.Zero ? null : hwnd;
}
readonly Popup popup = new();
readonly Grid fullScreenGrid = new();
readonly MediaPlayerElement mediaPlayerElement;
readonly CustomTransportControls? customTransportControls;
bool doesNavigationBarExistBeforeFullScreen;
bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="MauiMediaElement"/> class.
/// </summary>
/// <param name="mediaPlayerElement"></param>
public MauiMediaElement(MediaPlayerElement mediaPlayerElement)
{
LoadResourceDictionary();
this.mediaPlayerElement = mediaPlayerElement;
customTransportControls = SetTransportControls();
Children.Add(this.mediaPlayerElement);
}
void LoadResourceDictionary()
{
var assembly = Assembly.GetExecutingAssembly();
using Stream? stream = assembly.GetManifestResourceStream("ResourceDictionary.windows.xaml");
if (stream is null)
{
return;
}
using StreamReader reader = new(stream);
var xaml = reader.ReadToEnd();
var resourceDictionary = (Microsoft.UI.Xaml.ResourceDictionary)XamlReader.Load(xaml);
if (resourceDictionary is null)
{
return;
}
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
void ApplyCustomStyle()
{
if (this.Resources.TryGetValue("customTransportcontrols", out object styleObj) &&
styleObj is Microsoft.UI.Xaml.Style customStyle && mediaPlayerElement is not null && mediaPlayerElement.TransportControls is not null)
{
mediaPlayerElement.TransportControls.Style = customStyle;
}
}
CustomTransportControls SetTransportControls()
{
mediaPlayerElement.TransportControls.IsEnabled = false;
var temp = new CustomTransportControls()
{
IsZoomButtonVisible = true,
IsZoomEnabled = true,
IsVolumeButtonVisible = true,
IsVolumeEnabled = true,
IsSeekBarVisible = true,
IsSeekEnabled = true,
IsEnabled = true,
IsRepeatButtonVisible = true,
IsRepeatEnabled = true,
IsNextTrackButtonVisible = true,
IsPreviousTrackButtonVisible = true,
IsFastForwardButtonVisible = true,
IsFastForwardEnabled = true,
IsFastRewindButtonVisible = true,
IsFastRewindEnabled = true,
IsPlaybackRateButtonVisible = true,
IsPlaybackRateEnabled = true,
IsCompact = false,
};
temp.OnTemplateLoaded += (s, e) =>
{
if (temp.FullScreenButton is null)
{
return;
}
temp.FullScreenButton.Click += OnFullScreenButtonClick;
};
mediaPlayerElement.TransportControls = temp;
ApplyCustomStyle();
return temp;
}
/// <summary>
/// Finalizer
/// </summary>
~MauiMediaElement() => Dispose(false);
/// <summary>
/// Releases the managed and unmanaged resources used by the <see cref="MauiMediaElement"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Gets the presented page.
/// </summary>
protected static Page CurrentPage =>
PageExtensions.GetCurrentPage(Application.Current?.Windows[0].Page ?? throw new InvalidOperationException($"{nameof(Page)} cannot be null."));
/// <summary>
/// Releases the managed and unmanaged resources used by the <see cref="MauiMediaElement"/>.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
if (customTransportControls?.FullScreenButton is not null)
{
customTransportControls.FullScreenButton.Click -= OnFullScreenButtonClick;
}
if (disposing)
{
mediaPlayerElement.MediaPlayer.Pause();
if (mediaPlayerElement.MediaPlayer.Source is Windows.Media.Core.MediaSource mediaSource)
{
// Dispose the MediaSource to release the resources
// https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/play-audio-and-video-with-mediaplayer Shows how to dispose the MediaSource
mediaSource.Dispose();
}
mediaPlayerElement.MediaPlayer.Source = null;
mediaPlayerElement.MediaPlayer.Dispose();
mediaPlayerElement.SetMediaPlayer(null);
}
isDisposed = true;
}
static AppWindow GetAppWindowForCurrentWindow()
{
var windowHandle = TryGetForegroundWindow() ?? throw new InvalidOperationException("No foreground window found.");
var id = Win32Interop.GetWindowIdFromWindow(windowHandle);
return AppWindow.GetFromWindowId(id);
}
void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
{
var currentPage = CurrentPage;
var appWindow = GetAppWindowForCurrentWindow();
if (appWindow.Presenter.Kind is AppWindowPresenterKind.FullScreen)
{
appWindow.SetPresenter(AppWindowPresenterKind.Default);
Shell.SetNavBarIsVisible(CurrentPage, doesNavigationBarExistBeforeFullScreen);
if (popup.IsOpen)
{
popup.IsOpen = false;
popup.Child = null;
fullScreenGrid.Children.Clear();
}
Children.Add(mediaPlayerElement);
var parent = mediaPlayerElement.Parent as FrameworkElement;
mediaPlayerElement.Width = parent?.Width ?? mediaPlayerElement.Width;
mediaPlayerElement.Height = parent?.Height ?? mediaPlayerElement.Height;
customTransportControls?.FullScreenButton.Icon = new FontIcon { Glyph = "\uE740" };
}
else
{
appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
doesNavigationBarExistBeforeFullScreen = Shell.GetNavBarIsVisible(currentPage);
Shell.SetNavBarIsVisible(CurrentPage, false);
var displayInfo = DeviceDisplay.Current.MainDisplayInfo;
mediaPlayerElement.Width = displayInfo.Width / displayInfo.Density;
mediaPlayerElement.Height = displayInfo.Height / displayInfo.Density;
Children.Clear();
fullScreenGrid.Children.Add(mediaPlayerElement);
popup.XamlRoot = mediaPlayerElement.XamlRoot;
popup.HorizontalOffset = 0;
popup.VerticalOffset = 0;
popup.ShouldConstrainToRootBounds = false;
popup.VerticalAlignment = Microsoft.UI.Xaml.VerticalAlignment.Center;
popup.HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Center;
popup.Child = fullScreenGrid;
if (!popup.IsOpen)
{
popup.IsOpen = true;
}
customTransportControls?.FullScreenButton.Icon = new SymbolIcon(Symbol.BackToWindow);
}
}
}