Skip to content

Commit

Permalink
Fixed metadata when using URI based media fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
YourOrdinaryCat committed Jun 28, 2023
1 parent d59f333 commit d83b210
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 87 deletions.
32 changes: 15 additions & 17 deletions Rise Media Player Dev/Dialogs/MusicStreamingDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
}
27 changes: 12 additions & 15 deletions Rise Media Player Dev/Dialogs/VideoStreamingDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,41 +24,40 @@ 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;
InvalidUrlText.Visibility = Visibility.Visible;
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();
}
}
}
4 changes: 1 addition & 3 deletions Rise Media Player Dev/ViewModels/SongViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,7 +396,7 @@ public async Task<MediaPlaybackItem> AsPlaybackItemAsync()
return await file.GetSongAsync();
}

return await WebHelpers.GetSongFromUriAsync(uri, Title, Artist, Thumbnail, !uri.IsFile);
return await WebHelpers.GetSongFromUriAsync(uri, Title, Artist, Thumbnail);
}
#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion Rise Media Player Dev/ViewModels/VideoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public async Task<MediaPlaybackItem> AsPlaybackItemAsync()
return await file.GetVideoAsync();
}

return await WebHelpers.GetVideoFromUriAsync(uri, Title, Directors, Thumbnail, !uri.IsFile);
return await WebHelpers.GetVideoFromUriAsync(uri, Title, Directors, Thumbnail);
}
#endregion
}
Expand Down
134 changes: 83 additions & 51 deletions Rise.Common/Helpers/WebHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Rise.Common.Constants;
using Rise.Common.Extensions;
using System;
using System.Threading.Tasks;
using Windows.Media;
Expand All @@ -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);
}

/// <summary>
/// Gets a <see cref="MediaPlaybackItem"/> from the provided song Uri.
/// Creates a playback item which sources from the provided song URI.
/// </summary>
public static async Task<MediaPlaybackItem> GetSongFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null, bool fetchInfo = false)
public static async Task<MediaPlaybackItem> 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;
}

/// <summary>
/// Gets a <see cref="MediaPlaybackItem"/> from the provided video Uri.
/// Creates a playback item which sources from the provided song URI,
/// and uses the provided metadata for display properties.
/// </summary>
public static Task<MediaPlaybackItem> 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);
}

/// <summary>
/// Creates a playback item which sources from the provided video URI.
/// </summary>
public static async Task<MediaPlaybackItem> GetVideoFromUriAsync(Uri uri, string title = null, string subtitle = null, string thumbnail = null, bool fetchInfo = false)
public static async Task<MediaPlaybackItem> 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;
}

/// <summary>
/// Creates a playback item which sources from the provided video URI,
/// and uses the provided metadata for display properties.
/// </summary>
public static Task<MediaPlaybackItem> 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;
}
}
Expand Down

0 comments on commit d83b210

Please sign in to comment.