diff --git a/Rise Media Player Dev/Dialogs/MusicStreamingDialog.xaml.cs b/Rise Media Player Dev/Dialogs/MusicStreamingDialog.xaml.cs index 33cbc678..17a41b0b 100644 --- a/Rise Media Player Dev/Dialogs/MusicStreamingDialog.xaml.cs +++ b/Rise Media Player Dev/Dialogs/MusicStreamingDialog.xaml.cs @@ -1,8 +1,7 @@ using Rise.Common.Helpers; using Rise.Data.ViewModels; using System; -using System.Globalization; -using System.Net; +using Windows.Media.Playback; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using YoutubeExplode; @@ -24,42 +23,41 @@ private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, Conten { var deferral = args.GetDeferral(); - var url = StreamingTextBox.Text; - - string title = null, subtitle = null, thumbnailUrl = null; - - if (!Uri.TryCreate(StreamingTextBox.Text, UriKind.RelativeOrAbsolute, out Uri uri)) + string url = StreamingTextBox.Text; + if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri uri)) { args.Cancel = true; InvalidUrlText.Visibility = Visibility.Visible; return; } - bool isYoutubeLink = url.Contains("youtube.com/watch"); + MediaPlaybackItem song; + bool isYoutubeLink = url.Contains("youtube.com/watch"); if (isYoutubeLink) { var youtubeClient = new YoutubeClient(); - var youtubeSong = await youtubeClient.Videos.GetAsync(url.Replace("music.youtube.com", "www.youtube.com")); + var youtubeVideo = await youtubeClient.Videos.GetAsync(url.Replace("music.youtube.com", "www.youtube.com")); - title = youtubeSong.Title; - subtitle = youtubeSong.Author.ChannelTitle; - thumbnailUrl = youtubeSong.Thumbnails.GetWithHighestResolution().Url; + string title = youtubeVideo.Title; + string subtitle = youtubeVideo.Author.ChannelTitle; + string thumbnailUrl = youtubeVideo.Thumbnails.GetWithHighestResolution().Url; var streams = await youtubeClient.Videos.Streams.GetManifestAsync(url); uri = new(streams.GetAudioStreams().GetWithHighestBitrate().Url); + song = await WebHelpers.GetSongFromUriAsync(uri, title, subtitle, thumbnailUrl); + } + else + { + song = await WebHelpers.GetSongFromUriAsync(uri); } - Hide(); + deferral?.Complete(); await ViewModel.ResetPlaybackAsync(); - - var song = await WebHelpers.GetSongFromUriAsync(uri, title, subtitle, thumbnailUrl, !isYoutubeLink); ViewModel.AddSingleItemToQueue(song); ViewModel.Player.Play(); - - deferral?.Complete(); } } } diff --git a/Rise Media Player Dev/Dialogs/VideoStreamingDialog.xaml.cs b/Rise Media Player Dev/Dialogs/VideoStreamingDialog.xaml.cs index 3fb935aa..22413d6e 100644 --- a/Rise Media Player Dev/Dialogs/VideoStreamingDialog.xaml.cs +++ b/Rise Media Player Dev/Dialogs/VideoStreamingDialog.xaml.cs @@ -1,9 +1,7 @@ using Rise.Common.Helpers; using Rise.Data.ViewModels; using System; -using System.Globalization; -using System.Linq; -using System.Net; +using Windows.Media.Playback; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using YoutubeExplode; @@ -26,9 +24,6 @@ private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, Conten var deferral = args.GetDeferral(); var url = StreamingTextBox.Text; - - string title = null, subtitle = null, thumbnailUrl = null; - if (!Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out Uri uri)) { args.Cancel = true; @@ -36,31 +31,33 @@ private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, Conten return; } - bool isYoutubeLink = url.Contains("youtube.com/watch"); + MediaPlaybackItem video; + bool isYoutubeLink = url.Contains("youtube.com/watch"); if (isYoutubeLink) { var youtubeClient = new YoutubeClient(); var youtubeVideo = await youtubeClient.Videos.GetAsync(url.Replace("music.youtube.com", "www.youtube.com")); - title = youtubeVideo.Title; - subtitle = youtubeVideo.Author.ChannelTitle; - thumbnailUrl = youtubeVideo.Thumbnails.GetWithHighestResolution().Url; + string title = youtubeVideo.Title; + string subtitle = youtubeVideo.Author.ChannelTitle; + string thumbnailUrl = youtubeVideo.Thumbnails.GetWithHighestResolution().Url; var streams = await youtubeClient.Videos.Streams.GetManifestAsync(url); uri = new(streams.GetMuxedStreams().GetWithHighestVideoQuality().Url); + video = await WebHelpers.GetVideoFromUriAsync(uri, title, subtitle, thumbnailUrl); + } + else + { + video = await WebHelpers.GetVideoFromUriAsync(uri); } - Hide(); + deferral?.Complete(); await ViewModel.ResetPlaybackAsync(); - - var video = await WebHelpers.GetVideoFromUriAsync(uri, title, subtitle, thumbnailUrl, !isYoutubeLink); ViewModel.AddSingleItemToQueue(video); ViewModel.Player.Play(); - - deferral?.Complete(); } } } diff --git a/Rise Media Player Dev/ViewModels/SongViewModel.cs b/Rise Media Player Dev/ViewModels/SongViewModel.cs index 06e8702a..5615b392 100644 --- a/Rise Media Player Dev/ViewModels/SongViewModel.cs +++ b/Rise Media Player Dev/ViewModels/SongViewModel.cs @@ -8,9 +8,7 @@ using Rise.Data.ViewModels; using Rise.Models; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Media.Playback; @@ -398,7 +396,7 @@ public async Task AsPlaybackItemAsync() return await file.GetSongAsync(); } - return await WebHelpers.GetSongFromUriAsync(uri, Title, Artist, Thumbnail, !uri.IsFile); + return await WebHelpers.GetSongFromUriAsync(uri, Title, Artist, Thumbnail); } #endregion } diff --git a/Rise Media Player Dev/ViewModels/VideoViewModel.cs b/Rise Media Player Dev/ViewModels/VideoViewModel.cs index 8ff39a20..a2003b80 100644 --- a/Rise Media Player Dev/ViewModels/VideoViewModel.cs +++ b/Rise Media Player Dev/ViewModels/VideoViewModel.cs @@ -215,7 +215,7 @@ public async Task AsPlaybackItemAsync() return await file.GetVideoAsync(); } - return await WebHelpers.GetVideoFromUriAsync(uri, Title, Directors, Thumbnail, !uri.IsFile); + return await WebHelpers.GetVideoFromUriAsync(uri, Title, Directors, Thumbnail); } #endregion } diff --git a/Rise.Common/Helpers/WebHelpers.cs b/Rise.Common/Helpers/WebHelpers.cs index 68085d7e..d564595a 100644 --- a/Rise.Common/Helpers/WebHelpers.cs +++ b/Rise.Common/Helpers/WebHelpers.cs @@ -1,4 +1,5 @@ using Rise.Common.Constants; +using Rise.Common.Extensions; using System; using System.Threading.Tasks; using Windows.Media; @@ -19,86 +20,117 @@ public static class WebHelpers public static bool IsInternetAccessAvailable() { ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile(); - return connectionProfile != null + return connectionProfile != null && (connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess || connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.ConstrainedInternetAccess); } /// - /// Gets a from the provided song Uri. + /// Creates a playback item which sources from the provided song URI. /// - public static async Task GetSongFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null, bool fetchInfo = false) + public static async Task GetSongFromUriAsync(Uri uri) { var source = MediaSource.CreateFromUri(uri); + await source.OpenAsync(); - if (fetchInfo) - await source.OpenAsync(); - else - { - source.CustomProperties["Title"] = title ?? "Online song"; - source.CustomProperties["Artists"] = "UnknownArtistResource"; - source.CustomProperties["Year"] = 0u; - } + var media = new MediaPlaybackItem(source); + var props = media.GetDisplayProperties(); + + props.Type = MediaPlaybackType.Music; + props.MusicProperties.Title = props.MusicProperties.Title.ReplaceIfNullOrWhiteSpace("Online song"); + props.MusicProperties.Artist = props.MusicProperties.Artist.ReplaceIfNullOrWhiteSpace("UnknownArtistResource"); + source.CustomProperties["Title"] = props.MusicProperties.Title; + source.CustomProperties["Artists"] = props.MusicProperties.Artist; source.CustomProperties["Length"] = source.Duration; - var media = new MediaPlaybackItem(source); - var props = media.GetDisplayProperties(); + props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(new(URIs.MusicThumb)); - if (fetchInfo) - { - source.CustomProperties["Title"] = title ?? props.MusicProperties.Title; - source.CustomProperties["Artists"] = subtitle ?? props.MusicProperties.Artist; - source.CustomProperties["Year"] = 0u; - } - else - { - props.Type = MediaPlaybackType.Music; - props.MusicProperties.Title = title ?? "Online song"; - props.MusicProperties.Artist = subtitle ?? "UnknownArtistResource"; - } - - props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(new(thumbnail ?? URIs.MusicThumb)); media.ApplyDisplayProperties(props); - return media; } /// - /// Gets a from the provided video Uri. + /// Creates a playback item which sources from the provided song URI, + /// and uses the provided metadata for display properties. + /// + public static Task GetSongFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null) + { + string actualTitle = title.ReplaceIfNullOrWhiteSpace("Online song"); + string actualSubtitle = subtitle.ReplaceIfNullOrWhiteSpace("UnknownArtistResource"); + + var media = GetMediaFromUri(uri, actualTitle, actualSubtitle); + var props = media.GetDisplayProperties(); + + props.Type = MediaPlaybackType.Music; + props.MusicProperties.Title = actualTitle; + props.MusicProperties.Artist = actualSubtitle; + + Uri thumb = new(thumbnail.ReplaceIfNullOrWhiteSpace(URIs.MusicThumb)); + props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(thumb); + + media.ApplyDisplayProperties(props); + return Task.FromResult(media); + } + + /// + /// Creates a playback item which sources from the provided video URI. /// - public static async Task GetVideoFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null, bool fetchInfo = false) + public static async Task GetVideoFromUriAsync(Uri uri) { var source = MediaSource.CreateFromUri(uri); + await source.OpenAsync(); + + var media = new MediaPlaybackItem(source); + var props = media.GetDisplayProperties(); - if (fetchInfo) - await source.OpenAsync(); - else - { - source.CustomProperties["Title"] = title ?? "Online video"; - source.CustomProperties["Artists"] = "UnknownArtistResource"; - source.CustomProperties["Year"] = 0u; - } + props.Type = MediaPlaybackType.Video; + props.VideoProperties.Title = props.VideoProperties.Title.ReplaceIfNullOrWhiteSpace("Online video"); + props.VideoProperties.Subtitle = props.VideoProperties.Subtitle.ReplaceIfNullOrWhiteSpace("UnknownArtistResource"); + source.CustomProperties["Title"] = props.VideoProperties.Title; + source.CustomProperties["Artists"] = props.VideoProperties.Subtitle; source.CustomProperties["Length"] = source.Duration; - var media = new MediaPlaybackItem(source); + props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(new(URIs.VideoThumb)); + + media.ApplyDisplayProperties(props); + return media; + } + + /// + /// Creates a playback item which sources from the provided video URI, + /// and uses the provided metadata for display properties. + /// + public static Task GetVideoFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null) + { + string actualTitle = title.ReplaceIfNullOrWhiteSpace("Online video"); + string actualSubtitle = subtitle.ReplaceIfNullOrWhiteSpace("UnknownArtistResource"); + + var media = GetMediaFromUri(uri, actualTitle, actualSubtitle); var props = media.GetDisplayProperties(); - if (fetchInfo) - { - source.CustomProperties["Title"] = title ?? props.VideoProperties.Title; - source.CustomProperties["Artists"] = subtitle ?? props.VideoProperties.Subtitle; - } else - { - props.Type = MediaPlaybackType.Video; - props.VideoProperties.Title = title ?? "Online video"; - props.VideoProperties.Subtitle = subtitle ?? "UnknownArtistResource"; - } - - props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(new(thumbnail ?? URIs.VideoThumb)); + props.Type = MediaPlaybackType.Video; + props.VideoProperties.Title = actualTitle; + props.VideoProperties.Subtitle = actualSubtitle; + + Uri thumb = new(thumbnail.ReplaceIfNullOrWhiteSpace(URIs.VideoThumb)); + props.Thumbnail ??= RandomAccessStreamReference.CreateFromUri(thumb); + media.ApplyDisplayProperties(props); + return Task.FromResult(media); + } + private static MediaPlaybackItem GetMediaFromUri(Uri uri, string title, string subtitle) + { + var source = MediaSource.CreateFromUri(uri); + + source.CustomProperties["Title"] = title; + source.CustomProperties["Artists"] = subtitle; + source.CustomProperties["Length"] = source.Duration; + source.CustomProperties["Year"] = 0u; + + var media = new MediaPlaybackItem(source); return media; } }