Add() public method

public Add ( IMediaFile item ) : void
item IMediaFile
return void
            public void Basic()
            {
                var queue = new MediaQueue();

                IList<NotifyCollectionChangedEventArgs> collectionChangedEvents = new List<NotifyCollectionChangedEventArgs>();
                IList<PropertyChangedEventArgs> propertyChangedEvents = new List<PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

                var tracks = new[]
                    {
                        new MediaFile(),
                        new MediaFile(),
                    };

                queue.Add(tracks[0]);

                Assert.AreEqual(1, queue.Count);
                Assert.AreEqual(tracks[0], queue.Current);
                Assert.AreEqual(0, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(false, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(3, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems.Count == 1 && e.OldItems == null).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);

                queue.Add(tracks[1]);

                Assert.AreEqual(2, queue.Count);
                Assert.AreEqual(tracks[0], queue.Current);
                Assert.AreEqual(0, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(false, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(2, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(4, propertyChangedEvents.Count);
                Assert.AreEqual(2, collectionChangedEvents.Where(e => e.NewItems.Count == 1 && e.OldItems == null).Count());
                Assert.AreEqual(2, collectionChangedEvents.Count);
            }
示例#2
0
        public async Task Play(IMediaFile mediaFile = null)
        {
            if (mediaFile == null)
            {
                if (Status == MediaPlayerStatus.Paused)
                {
                    await Resume();

                    return;
                }

                mediaFile = CurrentMediaFile;
            }

            if (_currentPlaybackManager != null && Status == MediaPlayerStatus.Failed)
            {
                await PlayNext();

                return;
            }

            if (mediaFile == null)
            {
                await Play(MediaQueue);

                return;
            }

            if (!MediaQueue.Contains(mediaFile))
            {
                MediaQueue.Add(mediaFile);
            }

            MediaQueue.SetTrackAsCurrent(mediaFile);

            await RaiseMediaFileFailedEventOnException(async() =>
            {
                await PlayCurrent();
            });

            MediaNotificationManager?.StartNotification(mediaFile);
        }
        /// <summary>
        /// Adds MediaFile to the Queue and starts playing
        /// </summary>
        /// <param name="mediaFile"></param>
        /// <returns></returns>
        public async Task Play(IMediaFile mediaFile)
        {
            if (_currentPlaybackManager != null && Status == MediaPlayerStatus.Failed)
            {
                await PlayNext();

                return;
            }

            if (mediaFile == null)
            {
                await Play(MediaQueue);

                return;
            }

            if (!MediaQueue.Contains(mediaFile))
            {
                MediaQueue.Add(mediaFile);
            }

            MediaQueue.SetTrackAsCurrent(mediaFile);

            try
            {
                var beforePlayTask = _onBeforePlay?.Invoke(_currentMediaFile);
                if (beforePlayTask != null)
                {
                    await beforePlayTask;
                }
                await CurrentPlaybackManager.Play(mediaFile);
                await GetMediaInformation(new[] { mediaFile });

                MediaNotificationManager?.StartNotification(mediaFile);
            }
            catch (Exception ex)
            {
                OnMediaFileFailed(this, new MediaFileFailedEventArgs(ex, mediaFile));
            }
        }
            public void WhenAddingItemsWhileShuffled_OrderIsSameAsBeforeButWithExtraItem()
            {
                var arr = new[]
                              {
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile(),
                                  new MediaFile()
                              };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();
                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.ToggleShuffle();
                queue.Add(new MediaFile());

                Console.WriteLine("Shuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                queue.ToggleShuffle();

                Console.WriteLine("Unshuffled: {0}", string.Join(",", queue.Cast<MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast<MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[0].Id, queue.Cast<MediaFile>().ElementAt(0).Id);
                Assert.AreEqual(arr[1].Id, queue.Cast<MediaFile>().ElementAt(1).Id);
                Assert.AreEqual(arr[2].Id, queue.Cast<MediaFile>().ElementAt(2).Id);
                Assert.AreEqual(arr[3].Id, queue.Cast<MediaFile>().ElementAt(3).Id);
                Assert.AreEqual(arr[4].Id, queue.Cast<MediaFile>().ElementAt(4).Id);
                Assert.AreEqual(arr[5].Id, queue.Cast<MediaFile>().ElementAt(5).Id);
                Assert.AreEqual(arr[6].Id, queue.Cast<MediaFile>().ElementAt(6).Id);
                Assert.AreEqual(arr[7].Id, queue.Cast<MediaFile>().ElementAt(7).Id);
                Assert.AreEqual(arr[8].Id, queue.Cast<MediaFile>().ElementAt(8).Id);
                Assert.AreEqual(arr[9].Id, queue.Cast<MediaFile>().ElementAt(9).Id);
                Assert.AreEqual(arr[10].Id, queue.Cast<MediaFile>().ElementAt(10).Id);
                Assert.AreEqual(arr.Length + 1, queue.Count, "The array length is different");
            }