示例#1
0
        public static async Task <RandomAccessStreamReference> GetCover(this Album album, int size, CancellationToken cancellationToken = default)
        {
            if (!album.LibraryImages.Any())
            {
                return(null);
            }


            var song = album.LibraryImages.First();

            if (song.imageId is null || song.providerId is null)
            {
                return(null);
            }
            var image = await LibraryRegistry <MediaSource, Uri> .Get(song.providerId).GetImage(song.imageId, size, cancellationToken);

            if (image is null)
            {
                return(null);
            }
            else
            {
                var path = image.ToString();
                if (path.StartsWith("file:///"))
                {
                    path = path.Substring("file:///".Length).Replace('/', '\\');
                }

                return(RandomAccessStreamReference.CreateFromFile(await StorageFile.GetFileFromPathAsync(path)));
            }
            //return RandomAccessStreamReference.CreateFromUri(image);
        }
示例#2
0
        public static async Task <RandomAccessStreamReference> GetCover(this Song song, int size, CancellationToken cancellationToken = default)
        {
            if (song.LibraryImageId is null)
            {
                return(null);
            }
            var image = await LibraryRegistry <MediaSource, Uri> .Get(song.LibraryProvider).GetImage(song.LibraryImageId, size, cancellationToken);

            if (image is null)
            {
                return(null);
            }
            else
            {
                var path = image.ToString();
                if (path.StartsWith("file:///"))
                {
                    path = path.Substring("file:///".Length).Replace('/', '\\');
                }

                var file = await StorageFile.GetFileFromPathAsync(path);

                return(RandomAccessStreamReference.CreateFromFile(file));
            }
        }
示例#3
0
        public static async Task <ImageSource> GetCoverImageSource(this Song song, int size, CancellationToken cancellationToken = default)
        {
            if (song.LibraryImageId is null)
            {
                return(null);
            }
            var uri = await LibraryRegistry <MediaSource, Uri> .Get(song.LibraryProvider).GetImage(song.LibraryImageId, size, cancellationToken);

            if (uri is null)
            {
                return(null);
            }
            else
            {
                return(new BitmapImage(uri));
            }
        }
示例#4
0
        public static async Task <ImageSource> GetCoverImageSource(this Album album, int size, CancellationToken cancellationToken = default)
        {
            if (!album.LibraryImages.Any())
            {
                return(null);
            }


            var albumCoverName = GetAlbumCoverName(album);

            var coverFolder = await GetAlbumStorageFolder();

            var cover = await coverFolder.TryGetItemAsync(albumCoverName);

            Uri uri = null;

            if (cover != null)
            {
                var properties = await cover.GetBasicPropertiesAsync();

                if (properties.Size > 0)
                {
                    uri = new Uri(cover.Path);
                }
            }

            if (uri is null)
            {
                var song = album.LibraryImages.First();
                if (song.imageId is null || song.providerId is null)
                {
                    return(null);
                }
                uri = await LibraryRegistry <MediaSource, Uri> .Get(song.providerId).GetImage(song.imageId, size, cancellationToken);
            }

            if (uri is null)
            {
                return(null);
            }
            else
            {
                return(new BitmapImage(uri));
            }
        }
示例#5
0
        private async Task <PlayingSong> AddSongInternal(Song song)
        {
            Task                       getCover;
            MediaSource                media;
            List <PlayingSong>         list;
            MediaItemDisplayProperties oldProperties = null;

            if (this.mediaItemLookup.ContainsKey(song))
            {
                list = this.mediaItemLookup[song];
                var oldMedia = list.FirstOrDefault()?.MediaPlaybackItem;
                media = oldMedia?.Source;
                if (media is null) // Async could add list before mediasource is added.
                {
                    media = await LibraryRegistry <MediaSource, Uri> .Get(song.LibraryProvider).GetMediaSource(song.MediaId, default);
                }
                else
                {
                    oldProperties = oldMedia.GetDisplayProperties();
                }
            }
            else
            {
                list = new List <PlayingSong>();
                this.mediaItemLookup.Add(song, list);
                media = await LibraryRegistry <MediaSource, Uri> .Get(song.LibraryProvider).GetMediaSource(song.MediaId, default);
            }
            if (media is null)
            {
                return(null);
            }
            var mediaItem = new MediaPlaybackItem(media);


            var viewModel = new PlayingSong(mediaItem, song);

            this.playbackItemLookup.Add(mediaItem, viewModel);
            list.Add(viewModel);

            if (oldProperties is null)
            {
                var displayProperties = mediaItem.GetDisplayProperties();
                displayProperties.Type = Windows.Media.MediaPlaybackType.Music;
                displayProperties.MusicProperties.AlbumTitle  = song.AlbumName;
                displayProperties.MusicProperties.TrackNumber = (uint)song.Track;
                displayProperties.MusicProperties.Title       = song.Title;

                displayProperties.MusicProperties.Genres.Clear();
                foreach (var genre in song.Genres)
                {
                    displayProperties.MusicProperties.Genres.Add(genre);
                }

                displayProperties.MusicProperties.Artist = string.Join(", ", song.Interpreters);

                mediaItem.ApplyDisplayProperties(displayProperties);

                var coverTask = song.GetCover(300, default);
                getCover = coverTask.ContinueWith(t =>
                {
                    var coverStreamReferance    = t.Result;
                    displayProperties.Thumbnail = coverStreamReferance;
                    mediaItem.ApplyDisplayProperties(displayProperties);
                });
            }
            else
            {
                getCover = null;
            }

            this.mediaPlaybackList.Items.Add(mediaItem);

            int indexAdded;

            if (this.mediaPlaybackList.ShuffleEnabled)
            {
                indexAdded = this.mediaPlaybackList.ShuffledItems.Select((value, index) => (value, index)).First(x => x.value == mediaItem).index;
            }
            else
            {
                indexAdded = this.mediaPlaybackList.Items.Count - 1;
            }

            this.currentPlaylist.Insert(indexAdded, viewModel);

            if (getCover != null)
            {
                await getCover;
            }
            return(viewModel);
        }