Esempio n. 1
0
        public async Task RemoveSong(PlayingSong song)
        {
            if (!this.Dispatcher.HasThreadAccess)
            {
                var completionSource = new TaskCompletionSource <object>();
                await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                {
                    await this.RemoveSong(song);
                    completionSource.SetResult(null);
                });

                await completionSource.Task;
            }
            else
            {
                using (await this.semaphore.Lock())
                {
                    this.mediaPlaybackList.Items.Remove(song.MediaPlaybackItem);
                    this.playbackItemLookup.Remove(song.MediaPlaybackItem);
                    var list = this.mediaItemLookup[song.Song];
                    this.currentPlaylist.Remove(song);
                    list.Remove(song);
                    if (list.Count == 0)
                    {
                        this.mediaItemLookup.Remove(song.Song);
                    }
                }
            }
        }
Esempio n. 2
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);
        }