Esempio n. 1
0
        /// <summary>
        /// The state change handler that defines what happens whenever the player changes state
        /// </summary>
        private void StateChanged()
        {
            if (State != PlayerState.Playing)
            {
                playLifetime = null;
            }

            if (State == PlayerState.Playing)
            {
                if (duration.HasValue == false)
                {
                    throw new InvalidOperationException("Playback is not permitted before a video is loaded");
                }

                playStartPosition = TimeSpan.FromSeconds(playerProgressBar.PlayCursorPosition * duration.Value.TotalSeconds);
                playStartTime     = DateTime.UtcNow;

                // start a play loop for as long as the state remains unchanged
                this.playLifetime = this.GetPropertyValueLifetime(nameof(State));
                playLifetime.LifetimeManager.Manage(Application.SetInterval(() =>
                {
                    if (State != PlayerState.Playing)
                    {
                        return;
                    }

                    var now                              = DateTime.UtcNow;
                    var delta                            = now - playStartTime;
                    var newPlayerPosition                = playStartPosition + delta;
                    var videoLocationPercentage          = Math.Round(100.0 * newPlayerPosition.TotalSeconds / duration.Value.TotalSeconds, 1);
                    videoLocationPercentage              = Math.Min(videoLocationPercentage, 100);
                    playerProgressBar.PlayCursorPosition = videoLocationPercentage / 100.0;
                    playButton.Text                      = $"Pause".ToConsoleString();

                    ConsoleBitmap seekedImage;
                    if (newPlayerPosition > duration)
                    {
                        State = PlayerState.Stopped;
                    }
                    else if (inMemoryVideo.TrySeek(newPlayerPosition, out seekedImage) == false)
                    {
                        State = PlayerState.Buffering;
                    }
                    else
                    {
                        CurrentFrame = seekedImage;
                    }
                }, TimeSpan.FromMilliseconds(1)));
            }
            else if (State == PlayerState.Stopped)
            {
                pictureFrame.BorderColor = ConsoleColor.Yellow;
                playButton.Text          = "Play".ToConsoleString();
            }
            else if (State == PlayerState.Paused)
            {
                playButton.Text = "Play".ToConsoleString();
            }
            else if (State == PlayerState.NotLoaded)
            {
                playButton.Text     = "Play".ToConsoleString();
                playButton.CanFocus = false;
            }
            else if (State == PlayerState.Buffering)
            {
                playButton.Text = "Play".ToConsoleString();
            }
            else if (State == PlayerState.Failed)
            {
                pictureFrame.BorderColor = ConsoleColor.Red;
                Dialog.ShowMessage(failedMessage.ToRed());
            }
            else
            {
                throw new Exception("Unknown state: " + State);
            }
        }