private async void TimerOnTick(object sender, EventArgs eventArgs)
        {
            try
            {
                var totalTicks = _startPositionTicks + PlayedVideoDuration.Ticks;

                Log.Info("Sending current runtime [{0}] to the server", totalTicks);

                var info = new PlaybackProgressInfo
                {
                    ItemId = _itemId,
                    IsMuted = false,
                    IsPaused = false,
                    PositionTicks = totalTicks
                };

                await _playbackManager.ReportPlaybackProgress(info, _streamInfo, false, ApiClient);
                SetPlaybackTicks(totalTicks);
            }
            catch (HttpException ex)
            {
                Utils.HandleHttpException("TimerOnTick()", ex, NavigationService, Log);
            }
        }
        public override void WireMessages()
        {
            Messenger.Default.Register<VideoMessage>(this, m =>
            {
                PlaylistItems = null;
                switch (m.PlayerSourceType)
                {
                    case PlayerSourceType.Video:
                        if (m.VideoItem != null)
                        {
                            SelectedItem = m.VideoItem;
                            PlaylistItems = null;
                        }
                        break;
                    case PlayerSourceType.Recording:
                        if (m.VideoItem != null)
                        {
                            RecordingItem = m.VideoItem;
                            PlaylistItems = null;
                        }
                        break;
                    case PlayerSourceType.Programme:
                        if (m.VideoItem != null)
                        {
                            ProgrammeItem = m.VideoItem;
                            PlaylistItems = null;
                        }
                        break;
                    case PlayerSourceType.Playlist:
                        PlaylistItems = m.VideoPlaylists;
                        SelectedItem = m.VideoItem;
                        break;
                }

                PlayerSourceType = m.PlayerSourceType;
                _isResume = m.IsResume;
                _startPositionTicks = m.ResumeTicks ?? 0;
            });

            Messenger.Default.Register<NotificationMessage>(this, async m =>
            {
                if (m.Notification.Equals(Constants.Messages.SetResumeMsg))
                {
                    _isResume = true;
                }

                if (m.Notification.Equals(Constants.Messages.SendVideoTimeToServerMsg))
                {
                    try
                    {
                        var totalTicks = _startPositionTicks + PlayedVideoDuration.Ticks;

                        Log.Info("Sending current runtime [{0}] to the server", totalTicks);

                        var info = new PlaybackStopInfo
                        {
                            ItemId = _itemId,
                            PositionTicks = totalTicks
                        };

                        await _playbackManager.ReportPlaybackStopped(info, _streamInfo, App.ServerInfo.Id, AuthenticationService.Current.LoggedInUserId, false, ApiClient);

                        SetPlaybackTicks(totalTicks);

                        if (_timer != null && _timer.IsEnabled)
                        {
                            _timer.Stop();
                        }

                        Messenger.Default.Send(new NotificationMessage(_itemId, totalTicks, Constants.Messages.RefreshResumeMsg));
                    }
                    catch (HttpException ex)
                    {
                        Utils.HandleHttpException("SendVideoTimeToServer", ex, NavigationService, Log);
                    }

                    if (!PlaylistItems.IsNullOrEmpty())
                    {
                        var index = PlaylistItems.IndexOf(SelectedItem);
                        if (index >= 0 && index < PlaylistItems.Count - 1)
                        {
                            SelectedItem = PlaylistItems[index + 1];
                            await InitiatePlayback(false);
                        }
                    }
                }

                if (m.Notification.Equals(Constants.Messages.VideoStateChangedMsg))
                {
                    try
                    {
                        var totalTicks = _startPositionTicks + PlayedVideoDuration.Ticks;
                        var isPaused = m.Sender != null && (bool)m.Sender;

                        if (_timer != null)
                        {
                            if (isPaused)
                            {
                                if (_timer.IsEnabled)
                                {
                                    _timer.Stop();
                                }
                            }
                            else
                            {
                                if (!_timer.IsEnabled)
                                {
                                    _timer.Start();
                                }
                            }
                        }

                        Log.Info("Sending current runtime [{0}] to the server", totalTicks);

                        var info = new PlaybackProgressInfo
                        {
                            IsMuted = false,
                            ItemId = _itemId,
                            PositionTicks = totalTicks,
                            IsPaused = isPaused
                        };

                        await ApiClient.ReportPlaybackProgressAsync(info);
                        SetPlaybackTicks(totalTicks);
                    }
                    catch (HttpException ex)
                    {
                        Utils.HandleHttpException("VideoStateChanged", ex, NavigationService, Log);
                    }
                }
            });
        }
        /// <summary>
        /// Timers the callback.
        /// </summary>
        /// <param name="state">The state.</param>
        private async void TimerCallback(object state)
        {
            var item = _mediaPlayer.CurrentMedia;

            if (item == null)
            {
                return;
            }

            var info = new PlaybackProgressInfo
            {
                ItemId = item.Id,
                UserId = _apiClient.CurrentUserId,
                IsMuted = _playback.IsMuted,
                IsPaused = _mediaPlayer.PlayState == PlayState.Paused,
                PositionTicks = _mediaPlayer.CurrentPositionTicks
            };

            try
            {
                await _apiClient.ReportPlaybackProgressAsync(info);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error sending playback progress checking for {0}", ex, item.Name);

                throw;
            }
        }
        /// <summary>
        /// Timers the callback.
        /// </summary>
        /// <param name="state">The state.</param>
        private async void TimerCallback(object state)
        {
            var item = _mediaPlayer.CurrentMedia;

            if (item == null)
            {
                return;
            }

            var info = new PlaybackProgressInfo
            {
                //SessionId = _sessionManager
                //Item = item,
                ItemId = item.Id,
                //MediaSourceId = string.Empty,
                IsMuted = _playback.IsMuted,
                IsPaused = _mediaPlayer.PlayState == PlayState.Paused,
                PositionTicks = _mediaPlayer.CurrentPositionTicks,
                CanSeek = _mediaPlayer.CanSeek,
                AudioStreamIndex = _mediaPlayer.CurrentAudioStreamIndex,
                SubtitleStreamIndex = _mediaPlayer.CurrentSubtitleStreamIndex,
                VolumeLevel = (_mediaPlayer.PlayState != PlayState.Idle) ? (int?) _playback.Volume : null,
                PlayMethod = PlayMethod.DirectPlay, // todo remove hard coding
            };

            var apiClient = _connectionManager.GetApiClient(item);

            try
            {
                await apiClient.ReportPlaybackProgressAsync(info);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error sending playback progress checking for {0}", ex, item.Name);
            }
        }
        /// <summary>
        /// Timers the callback.
        /// </summary>
        /// <param name="state">The state.</param>
        private async void TimerCallback(object state)
        {
            var item = _mediaPlayer.CurrentMedia;

            if (item == null)
            {
                return;
            }

            var info = new PlaybackProgressInfo
            {
                ItemId = item.Id,
                IsMuted = _internalPlaybackManager.IsMuted,
                IsPaused = _mediaPlayer.PlayState == PlayState.Paused,
                PositionTicks = _mediaPlayer.CurrentPositionTicks,
                CanSeek = _mediaPlayer.CanSeek,
                AudioStreamIndex = _mediaPlayer.CurrentAudioStreamIndex,
                SubtitleStreamIndex = _mediaPlayer.CurrentSubtitleStreamIndex,
                VolumeLevel = (_mediaPlayer.PlayState != PlayState.Idle) ? (int?) _internalPlaybackManager.Volume : null
            };

            var apiClient = _connectionManager.GetApiClient(item);

            var currentStreamInfo = _mediaPlayer.CurrentStreamInfo;

            try
            {
                // Have to test this for null because external players are currently not supplying this
                // Also some players will play in contexts not currently supported by common playback managers, e.g. direct play of folder rips, and iso-mounted media
                // Remove when implemented
                if (currentStreamInfo != null)
                {
                    info.PlayMethod = currentStreamInfo.PlayMethod;

                    info.MediaSourceId = currentStreamInfo.MediaSourceId;

                    await _apiPlaybackManager.ReportPlaybackProgress(info, currentStreamInfo, false, apiClient);
                }
                else
                {
                    await apiClient.ReportPlaybackProgressAsync(info);
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error sending internalPlaybackManager progress checking for {0}", ex, item.Name);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reports playback progress
        /// </summary>
        /// <param name="info">The information.</param>
        /// <param name="streamInfo">The stream information.</param>
        /// <param name="isOffline">if set to <c>true</c> [is offline].</param>
        /// <param name="apiClient">The current apiClient. It can be null if offline</param>
        /// <returns>Task.</returns>
        public async Task ReportPlaybackProgress(PlaybackProgressInfo info, StreamInfo streamInfo, bool isOffline, IApiClient apiClient)
        {
            if (!isOffline)
            {
                if (streamInfo != null)
                {
                    info.PlaySessionId = streamInfo.PlaySessionId;

                    if (streamInfo.MediaSource != null)
                    {
                        info.LiveStreamId = streamInfo.MediaSource.LiveStreamId;
                    }
                }

                await apiClient.ReportPlaybackProgressAsync(info).ConfigureAwait(false);
            }
        }
        private static async void DispatcherTimerOnTick(object sender, EventArgs eventArgs)
        {
            if (_apiClient == null || BackgroundAudioPlayer.Instance.PlayerState != PlayState.Playing)
            {
                return;
            }

            try
            {
                var track = BackgroundAudioPlayer.Instance.Track;
                var info = new PlaybackProgressInfo
                {
                    ItemId = track.Tag,
                    //UserId = _apiClient.CurrentUserId,
                    PositionTicks = BackgroundAudioPlayer.Instance.Position.Ticks,
                    IsMuted = false,
                    IsPaused = false
                };

                await _apiClient.ReportPlaybackProgressAsync(info);
            }
            catch (HttpException ex)
            {
                _logger.FatalException("DispatcherTimerOnTick()", ex);
            }
        }