-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainPage.xaml.cs
82 lines (66 loc) · 2.04 KB
/
MainPage.xaml.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
82
using CommunityToolkit.Mvvm.Messaging;
using MElemetModified.Services;
using Mopups.Services;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;
namespace MElemetModified;
public partial class MainPage : ContentPage
{
public Uri? MediaSource { get; set; }
DeviceOrientationService deviceOrientationService;
public MainPage()
{
InitializeComponent();
BindingContext = this;
deviceOrientationService = new();
#if ANDROID
btnFullScreen.IsVisible = true;
#elif IOS
btnFullScreen.IsVisible = false;
#endif
WeakReferenceMessenger.Default.Register<NotifyFullScreenClosed>(this, HandleOrientation);
}
/*
* https://youtu.be/dQw4w9WgXcQ
* here, videoId => dQw4w9WgXcQ
*/
protected override async void OnAppearing()
{
base.OnAppearing();
deviceOrientationService.SetDeviceOrientation(displayOrientation: DisplayOrientation.Portrait);
await Task.Run(async () =>
MediaSource = await CreateMediaStream(@"dQw4w9WgXcQ"));
if (MediaSource != null) await MainThread.InvokeOnMainThreadAsync((Action)(() => mediaElement.Source = MediaSource));
}
private void HandleOrientation(object recipient, NotifyFullScreenClosed message)
{
deviceOrientationService = new DeviceOrientationService();
deviceOrientationService.SetDeviceOrientation(displayOrientation: DisplayOrientation.Portrait);
}
protected override async void OnDisappearing()
{
base.OnDisappearing();
await Task.Run(() => MediaSource = null);
}
static async Task<Uri?> CreateMediaStream(string VideoId)
{
YoutubeClient _client = new();
var streamManifest = await _client.Videos.Streams.GetManifestAsync(VideoId);
var streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();
if (streamInfo != null) return new Uri(streamInfo.Url);
else
return null;
}
private async void btnFullScreen_Clicked(object sender, EventArgs e)
{
if (MediaSource != null)
{
FullScreenPage page = new(new CurrentVideoState
{
Position = mediaElement.Position,
VideoUri = MediaSource,
});
await MopupService.Instance.PushAsync(page);
}
}
}