Baseflow / XamarinMediaManager

Cross platform Xamarin plugin to play and control Audio and Video
https://baseflow.com
MIT License
769 stars 306 forks source link

Support for iOS MPMediaItems #746

Closed Hackmodford closed 4 years ago

Hackmodford commented 4 years ago

🚀 Feature Requests

Extension of MPMediaItem to convert to IMediaItem

Contextualize the feature

On iOS, I would like to perform a MPMediaQuery to play content synced via iTunes/Music/Books on the device.

Describe the feature

I tried passing in the assetUrl but it doesn't work 100% of the time. (I had errors loading metadata). One workaround is to just create a simple adapter class. Something like this?

public static IEnumerable<IMediaItem> CreateMediaItems(this IEnumerable<MPMediaItem> items)
{
    return items
        .Where(i => i.AssetURL != null && i.IsCloudItem == false && i.HasProtectedAsset == false)
        .Select(i => i.ToMediaItem());
}
public static IMediaItem ToMediaItem(this MPMediaItem item)
{

    var output = new MediaItem();
    output.Album = item.AlbumTitle;
    output.Artist = item.Artist;
    output.Compilation = null;
    output.Composer = item.Composer;

    if (item.ReleaseDate != null)
        output.Date = (DateTime)item.ReleaseDate;

    output.Duration = TimeSpan.FromSeconds(item.PlaybackDuration);
    output.Genre = item.Genre;

    if (item.Artwork != null)
        output.Image = item.Artwork.ImageWithSize(new CGSize(300, 300));
    // output.Rating = item.Rating;
    output.Title = item.Title;
    // output.Year = item.ReleaseDate.
    output.AlbumArtist = item.AlbumArtist;
    // output.AlbumImage =
    output.DiscNumber = item.DiscNumber;
    output.MediaUri = item.AssetURL.ToString();
    output.NumTracks = item.AlbumTrackCount;
    output.UserRating = item.Rating;
    // output.Id = item.PersistentID.ToString();
    return output;
}

This seems to be working. However I'm not sure how to handle the artwork as you need to specify a size for the UIImage.

If something like this already exists or will cause me headaches later, please let me know. I'm very new to this library but am willing to create a PR if this is worthy of adding to the project.

Platforms affected (mark all that apply)

martijn00 commented 4 years ago

Sounds like a good addition! Please open the PR and i'll have a look.

Hackmodford commented 4 years ago

I created a PR. I'm not sure if it builds as I'm on a really old slow mac laptop. Let me know if I need to move the file to a better location.

martijn00 commented 4 years ago

I had to work around it not being available on TvOS: https://github.com/Baseflow/XamarinMediaManager/blob/develop/MediaManager/Platforms/Ios/Media/MPMediaItemExtensions.cs