/// <summary>
        /// Loads a video from a given stream
        /// </summary>
        /// <param name="videoStream">the video stream</param>
        public void Load(Stream videoStream)
        {
            if (Application == null)
            {
                throw new InvalidOperationException("Can't load until the control has been added to an application");
            }

            Task.Factory.StartNew(() =>
            {
                try
                {
                    var reader = new ConsoleBitmapStreamReader(videoStream);
                    reader.ReadToEnd((videoWithProgressInfo) =>
                    {
                        inMemoryVideo = inMemoryVideo ?? videoWithProgressInfo;
                        this.duration = videoWithProgressInfo.Duration;
                        Application.InvokeNextCycle(() =>
                        {
                            if (this.CurrentFrame == null)
                            {
                                this.CurrentFrame = videoWithProgressInfo.Frames[0].Bitmap;
                                playerProgressBar.ShowPlayCursor = true;
                                playButton.CanFocus            = true;
                                seekToBeginningButton.CanFocus = true;
                                seekBack10SButton.CanFocus     = true;
                                seekForward10SButton.CanFocus  = true;
                                seekToEndButton.CanFocus       = true;
                                State = PlayerState.Stopped;
                                if (Application.FocusManager.FocusedControl == null)
                                {
                                    Application.FocusManager.TrySetFocus(playButton);
                                }
                            }

                            playerProgressBar.LoadProgressPosition = inMemoryVideo.LoadProgress;
                        });
                        if (AfterFrameLoadDelay.HasValue)
                        {
                            Thread.Sleep(AfterFrameLoadDelay.Value);
                        }
                    });
                }
                catch (Exception ex)
                {
#if DEBUG
                    failedMessage = ex.ToString();
#else
                    failedMessage = ex.Message;
#endif
                    Application.InvokeNextCycle(() =>
                    {
                        State = PlayerState.Failed;
                    });
                }
            });
        }
Exemple #2
0
 private bool TryOpenConsoleBitmapReaderFormat(string filePath)
 {
     try
     {
         using (var stream = File.OpenRead(filePath))
         {
             var reader    = new ConsoleBitmapStreamReader(stream);
             var animation = reader.ReadToEnd();
             this.animation.Frames.Clear();
             this.animation.Frames.AddRange(animation.Frames);
             CurrentFrameIndex = 0;
             return(true);
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemple #3
0
        private async void SaveCommon(string destination)
        {
            if (saveInProgress)
            {
                return;
            }

            try
            {
                saveInProgress = true;
                ConsoleBitmapVideoWriter writer = null;
                var  tempFile         = Path.GetTempFileName();
                bool tempWriteSuccess = false;
                try
                {
                    writer = new ConsoleBitmapVideoWriter(s => File.WriteAllText(tempFile, s));
                    foreach (var frame in animation.Frames)
                    {
                        writer.WriteFrame(frame.Bitmap, desiredFrameTime: frame.FrameTime);
                    }
                    tempWriteSuccess = true;
                }
                catch (Exception)
                {
                    await Dialog.ShowMessage($"Failed to save file to {destination}".ToRed());

                    return;
                }
                finally
                {
                    writer?.Finish();
                    if (tempWriteSuccess == false)
                    {
                        try { File.Delete(tempFile); } catch (Exception) { }
                    }
                }

                try
                {
                    // If we can't read this back then don't finalize the save.
                    // This ensures that if we wrote garbage (which should never happen)
                    // then we won't corrupt whatever was on disk.
                    using (var testStream = File.OpenRead(tempFile))
                    {
                        var testReader = new ConsoleBitmapStreamReader(testStream);
                        testReader.ReadToEnd();
                    }
                }
                catch (Exception)
                {
                    try { File.Delete(tempFile); } catch (Exception) { }
                    await Dialog.ShowMessage($"Failed to save file to {destination}".ToRed());

                    return;
                }

                File.Delete(destination);
                File.Move(tempFile, destination);
                currentlyOpenedFile = destination;
                undoRedo.Do(new ClearPendingChangesAction(this));
            }
            finally
            {
                saveInProgress = false;
            }
        }