Example #1
0
        public virtual void Update(MediaItem mediaItem)
        {
            if (!_mediaItem.Equals(mediaItem))
            {
                throw new ArgumentException("Update can only be done for the same MediaItem!", "mediaItem");
            }

            MediaItemAspect mediaAspect;

            if (mediaItem.Aspects.TryGetValue(MediaAspect.ASPECT_ID, out mediaAspect))
            {
                Title     = (string)mediaAspect[MediaAspect.ATTR_TITLE];
                Rating    = (int?)mediaAspect[MediaAspect.ATTR_RATING] ?? 0;
                PlayCount = (int?)mediaAspect[MediaAspect.ATTR_PLAYCOUNT] ?? 0;
            }
            FireChange();
        }
        public virtual void Update(MediaItem mediaItem)
        {
            if (!_mediaItem.Equals(mediaItem))
            {
                throw new ArgumentException("Update can only be done for the same MediaItem!", "mediaItem");
            }

            int?currentPlayCount = null;
            SingleMediaItemAspect mediaAspect;

            if (MediaItemAspect.TryGetAspect(mediaItem.Aspects, MediaAspect.Metadata, out mediaAspect))
            {
                Title            = (string)mediaAspect[MediaAspect.ATTR_TITLE];
                SortString       = (string)mediaAspect[MediaAspect.ATTR_SORT_TITLE];
                Rating           = (int?)mediaAspect[MediaAspect.ATTR_RATING] ?? 0;
                currentPlayCount = (int?)mediaAspect[MediaAspect.ATTR_PLAYCOUNT] ?? 0;
                Virtual          = (bool?)mediaAspect[MediaAspect.ATTR_ISVIRTUAL];
            }

            TimeSpan?duration = null;
            IList <MediaItemAspect> aspects;

            if (mediaItem.Aspects.TryGetValue(VideoStreamAspect.ASPECT_ID, out aspects))
            {
                var  aspect  = aspects.First();
                int? part    = (int?)aspect[VideoStreamAspect.ATTR_VIDEO_PART];
                int? partSet = (int?)aspect[VideoStreamAspect.ATTR_VIDEO_PART_SET];
                long?dur     = null;
                if (!part.HasValue || part < 0)
                {
                    dur = (long?)aspect[VideoStreamAspect.ATTR_DURATION];
                }
                else if (partSet.HasValue)
                {
                    dur = aspects.Where(a => (int?)a[VideoStreamAspect.ATTR_VIDEO_PART_SET] == partSet &&
                                        aspect[VideoStreamAspect.ATTR_DURATION] != null).Sum(a => (long)a[VideoStreamAspect.ATTR_DURATION]);
                }
                if (dur.HasValue)
                {
                    duration = TimeSpan.FromSeconds(dur.Value);
                }
            }
            else if (mediaItem.Aspects.TryGetValue(AudioAspect.ASPECT_ID, out aspects))
            {
                var  aspect = aspects.First();
                long?dur    = aspect == null ? null : (long?)aspect[AudioAspect.ATTR_DURATION];
                if (dur.HasValue)
                {
                    duration = TimeSpan.FromSeconds(dur.Value);
                }
            }
            else if (mediaItem.Aspects.TryGetValue(MovieAspect.ASPECT_ID, out aspects))
            {
                var aspect = aspects.First();
                int?dur    = aspect == null ? null : (int?)aspect[MovieAspect.ATTR_RUNTIME_M];
                if (dur.HasValue)
                {
                    duration = TimeSpan.FromMinutes(dur.Value);
                }
            }
            Duration = duration.HasValue ? FormattingUtils.FormatMediaDuration(duration.Value) : string.Empty;

            if (mediaItem.UserData.ContainsKey(UserDataKeysKnown.KEY_PLAY_PERCENTAGE))
            {
                WatchPercentage = mediaItem.UserData[UserDataKeysKnown.KEY_PLAY_PERCENTAGE];
            }
            else if (mediaItem.UserData.ContainsKey(PlayerContext.KEY_RESUME_STATE))
            {
                IResumeState        resumeState    = ResumeStateBase.Deserialize(mediaItem.UserData[PlayerContext.KEY_RESUME_STATE]);
                PositionResumeState positionResume = resumeState as PositionResumeState;
                if (positionResume != null && duration.HasValue)
                {
                    TimeSpan resumePosition = positionResume.ResumePosition;
                    if (duration.Value.TotalSeconds > 0)
                    {
                        WatchPercentage = ((int)(resumePosition.TotalSeconds * 100 / duration.Value.TotalSeconds)).ToString();
                    }
                    else if (currentPlayCount > 0)
                    {
                        WatchPercentage = "100";
                    }
                    else
                    {
                        WatchPercentage = "0";
                    }
                }
            }

            if (mediaItem.UserData.ContainsKey(UserDataKeysKnown.KEY_PLAY_COUNT))
            {
                PlayCount = Convert.ToInt32(mediaItem.UserData[UserDataKeysKnown.KEY_PLAY_COUNT]);
            }
            else if (currentPlayCount.HasValue)
            {
                PlayCount = currentPlayCount.Value;
            }

            FireChange();
        }