Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ async void ChangeSourceClicked(Object sender, EventArgs e)
var result = await DisplayActionSheet("Choose a source", "Cancel", null,
loadOnlineMp4, loadHls, loadLocalResource, resetSource, loadMusic);

MediaElement.Stop();
MediaElement.Source = null;

switch (result)
{
case loadOnlineMp4:
Expand Down Expand Up @@ -273,6 +276,7 @@ void DisplayPopup(object sender, EventArgs e)
popup.Closed += (s, e) =>
{
popupMediaElement.Stop();
popupMediaElement.Source = null;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,25 +538,45 @@ protected override void Dispose(bool disposing)

static async Task<byte[]> GetBytesFromMetadataArtworkUrl(string? url, CancellationToken cancellationToken = default)
{
byte[] artworkData = [];
Stream? stream = null;
try
{
var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
var stream = response.IsSuccessStatusCode ? await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false) : null;

if (stream is null)
byte[] artworkData = [];
if (Uri.TryCreate(url!, UriKind.Absolute, out var uri))
{
return artworkData;
}
int contentLength = 0;
if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)
{
var request = new HttpRequestMessage(HttpMethod.Head, url);
var contentLengthResponse = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
contentLength = (int)(contentLengthResponse.Content.Headers.ContentLength ?? 0);
var response = await client.GetAsync(url, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
stream = response.IsSuccessStatusCode ? await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false) : null;
}
else if (uri.Scheme == Uri.UriSchemeFile)
{
stream = File.OpenRead(url!);
FileInfo fi = new(url);
contentLength = (int)fi.Length;
}

using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
var bytes = memoryStream.ToArray();
return bytes;
if (stream is not null)
{
// allocate memory once
artworkData = new byte[contentLength];
using var memoryStream = new MemoryStream(artworkData);
await stream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
}
}
return artworkData;
}
catch
catch (Exception)
{
return artworkData;
return [];
}
finally
{
stream?.Close();
}
}

Expand Down Expand Up @@ -636,7 +656,10 @@ void StopService(in BoundServiceConnection boundServiceConnection)
mediaMetaData.SetArtist(MediaElement.MetadataArtist);
mediaMetaData.SetTitle(MediaElement.MetadataTitle);
var data = await GetBytesFromMetadataArtworkUrl(MediaElement.MetadataArtworkUrl, cancellationToken).ConfigureAwait(true);
mediaMetaData.SetArtworkData(data, (Java.Lang.Integer)MediaMetadata.PictureTypeFrontCover);
if (data != null && data.Length > 0)
{
mediaMetaData.SetArtworkData(data, (Java.Lang.Integer)MediaMetadata.PictureTypeFrontCover);
}

mediaItem = new MediaItem.Builder();
mediaItem.SetUri(url);
Expand Down