-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathMediaElementPage.xaml.cs
More file actions
293 lines (239 loc) · 8.19 KB
/
MediaElementPage.xaml.cs
File metadata and controls
293 lines (239 loc) · 8.19 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using System.ComponentModel;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Sample.Constants;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Views;
using Microsoft.Extensions.Logging;
namespace CommunityToolkit.Maui.Sample.Pages.Views;
public partial class MediaElementPage : BasePage<MediaElementViewModel>
{
const string loadOnlineMp4 = "Load Online MP4";
const string loadHls = "Load HTTP Live Stream (HLS)";
const string loadLocalResource = "Load Local Resource";
const string resetSource = "Reset Source to null";
const string loadMusic = "Load Music";
const string botImageUrl = "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm";
const string hlsStreamTestUrl = "https://mtoczko.github.io/hls-test-streams/test-gap/playlist.m3u8";
const string hal9000AudioUrl = "https://github.com/prof3ssorSt3v3/media-sample-files/raw/master/hal-9000.mp3";
readonly ILogger logger;
readonly IDeviceInfo deviceInfo;
readonly IFileSystem fileSystem;
public MediaElementPage(MediaElementViewModel viewModel, IFileSystem fileSystem, IDeviceInfo deviceInfo, ILogger<MediaElementPage> logger) : base(viewModel)
{
InitializeComponent();
this.logger = logger;
this.deviceInfo = deviceInfo;
this.fileSystem = fileSystem;
MediaElement.PropertyChanged += MediaElement_PropertyChanged;
}
void MediaElement_FullScreenStateChanged(object? sender, ScreenStateChangedEventArgs e) =>
logger.LogInformation("FullScreen State Changed. Old State: {PreviousState}, New State: {NewState}", e.PreviousState, e.NewState);
void MediaElement_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == MediaElement.DurationProperty.PropertyName)
{
logger.LogInformation("Duration: {NewDuration}", MediaElement.Duration);
PositionSlider.Maximum = MediaElement.Duration.TotalSeconds;
}
}
void OnMediaOpened(object? sender, EventArgs? e) => logger.LogInformation("Media opened.");
void OnStateChanged(object? sender, MediaStateChangedEventArgs e) =>
logger.LogInformation("Media State Changed. Old State: {PreviousState}, New State: {NewState}", e.PreviousState, e.NewState);
void OnMediaFailed(object? sender, MediaFailedEventArgs e) => logger.LogInformation("Media failed. Error: {ErrorMessage}", e.ErrorMessage);
void OnMediaEnded(object? sender, EventArgs? e) => logger.LogInformation("Media ended.");
void OnPositionChanged(object? sender, MediaPositionChangedEventArgs e)
{
logger.LogInformation("Position changed to {Position}", e.Position);
PositionSlider.Value = e.Position.TotalSeconds;
}
void OnSeekCompleted(object? sender, EventArgs? e) => logger.LogInformation("Seek completed.");
void OnSpeedMinusClicked(object? sender, EventArgs? e)
{
if (MediaElement.Speed >= 1)
{
MediaElement.Speed -= 1;
}
}
void OnSpeedPlusClicked(object? sender, EventArgs? e)
{
if (MediaElement.Speed < 10)
{
MediaElement.Speed += 1;
}
}
void OnVolumeMinusClicked(object? sender, EventArgs? e)
{
if (MediaElement.Volume >= 0)
{
if (MediaElement.Volume < .1)
{
MediaElement.Volume = 0;
return;
}
MediaElement.Volume -= .1;
}
}
void OnVolumePlusClicked(object? sender, EventArgs? e)
{
if (MediaElement.Volume < 1)
{
if (MediaElement.Volume > .9)
{
MediaElement.Volume = 1;
return;
}
MediaElement.Volume += .1;
}
}
void OnPlayClicked(object? sender, EventArgs? e)
{
MediaElement.Play();
}
void OnPauseClicked(object? sender, EventArgs? e)
{
MediaElement.Pause();
}
void OnStopClicked(object? sender, EventArgs? e)
{
MediaElement.Stop();
}
void OnMuteClicked(object? sender, EventArgs? e)
{
MediaElement.ShouldMute = !MediaElement.ShouldMute;
}
protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
{
base.OnNavigatedFrom(args);
MediaElement.Stop();
MediaElement.Handler?.DisconnectHandler();
}
async void Slider_DragCompleted(object? sender, EventArgs? e)
{
ArgumentNullException.ThrowIfNull(sender);
var newValue = ((Slider)sender).Value;
await MediaElement.SeekTo(TimeSpan.FromSeconds(newValue), CancellationToken.None);
MediaElement.Play();
}
void Slider_DragStarted(object? sender, EventArgs? e)
{
MediaElement.Pause();
}
async void Button_Clicked(object? sender, EventArgs? e)
{
if (string.IsNullOrWhiteSpace(CustomSourceEntry.Text))
{
await DisplayAlertAsync("Error Loading URL Source", "No value was found to load as a media source. " +
"When you do enter a value, make sure it's a valid URL. No additional validation is done.",
"OK");
return;
}
MediaElement.Source = MediaSource.FromUri(CustomSourceEntry.Text);
}
async void ChangeSourceClicked(object? sender, EventArgs? e)
{
var result = await DisplayActionSheetAsync("Choose a source", "Cancel", null,
loadOnlineMp4, loadHls, loadLocalResource, resetSource, loadMusic);
MediaElement.Stop();
MediaElement.Source = null;
switch (result)
{
case loadOnlineMp4:
MediaElement.MetadataTitle = "Big Buck Bunny";
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataArtist = "Big Buck Bunny Album";
MediaElement.Source =
MediaSource.FromUri(StreamingVideoUrls.BuckBunny);
return;
case loadHls:
MediaElement.MetadataArtist = "HLS Album";
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataTitle = "HLS Title";
MediaElement.Source = MediaSource.FromUri(hlsStreamTestUrl);
return;
case resetSource:
MediaElement.MetadataArtworkUrl = string.Empty;
MediaElement.MetadataTitle = string.Empty;
MediaElement.MetadataArtist = string.Empty;
MediaElement.Source = null;
return;
case loadLocalResource:
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataTitle = "Local Resource Title";
MediaElement.MetadataArtist = "Local Resource Album";
if (DeviceInfo.Platform == DevicePlatform.MacCatalyst
|| DeviceInfo.Platform == DevicePlatform.iOS)
{
MediaElement.Source = MediaSource.FromResource("AppleVideo.mp4");
}
else if (DeviceInfo.Platform == DevicePlatform.Android)
{
MediaElement.Source = MediaSource.FromResource("AndroidVideo.mp4");
}
else if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
MediaElement.Source = MediaSource.FromResource("WindowsVideo.mp4");
}
return;
case loadMusic:
MediaElement.MetadataTitle = "HAL 9000";
MediaElement.MetadataArtist = "HAL 9000 Album";
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.Source = MediaSource.FromUri(hal9000AudioUrl);
return;
}
}
async void ChangeAspectClicked(object? sender, EventArgs? e)
{
const string cancel = "Cancel";
var resultAspect = await DisplayActionSheetAsync(
"Choose aspect ratio",
cancel,
null,
Aspect.AspectFit.ToString(),
Aspect.AspectFill.ToString(),
Aspect.Fill.ToString());
if (resultAspect is null or cancel)
{
return;
}
if (!Enum.TryParse(typeof(Aspect), resultAspect, true, out var aspectEnum))
{
await DisplayAlertAsync("Error", "There was an error determining the selected aspect", "OK");
return;
}
MediaElement.Aspect = (Aspect)aspectEnum;
}
async void DisplayPopup(object? sender, EventArgs? e)
{
MediaElement.Pause();
MediaSource source;
if (deviceInfo.Platform == DevicePlatform.Android)
{
source = MediaSource.FromResource("AndroidVideo.mp4");
}
else if (deviceInfo.Platform == DevicePlatform.MacCatalyst
|| deviceInfo.Platform == DevicePlatform.iOS
|| deviceInfo.Platform == DevicePlatform.macOS)
{
source = MediaSource.FromResource("AppleVideo.mp4");
}
else
{
source = MediaSource.FromResource("WindowsVideo.mp4");
}
var popupMediaElement = new MediaElement
{
WidthRequest = 600,
HeightRequest = 400,
AndroidViewType = AndroidViewType.SurfaceView,
Source = source,
MetadataArtworkUrl = botImageUrl,
ShouldAutoPlay = true,
ShouldShowPlaybackControls = true,
};
await this.ShowPopupAsync(popupMediaElement);
popupMediaElement.Stop();
popupMediaElement.Source = null;
}
}