public void Stop() { if (VideoPlayerElement.Source != null) { VideoPlayerElement.Stop(); this.Background = Brushes.DarkGray; MediaState = MediaState.Stop; } }
/// <summary> /// Sometimes MediaElement crashes - black window /// I will replace it with the new one /// </summary> internal void RecreateMediaElement(bool flipHorizontally) { try { if (VideoPlayerElement != null) { VideoPlayerElement.Stop(); VideoPlayerElement.Close(); VideoPlayerElement.Clock = null; VideoPlayerElement.Source = null; VideoPlayerElement.Volume = 0; VideoPlayerElement = null; scrollPlayer.Content = null; GC.Collect(); GC.WaitForPendingFinalizers(); } MediaState = MediaState.Manual; VideoPlayerElement = new MediaElement(); VideoPlayerElement.Width = 1920; VideoPlayerElement.Height = 1080; VideoPlayerElement.LoadedBehavior = MediaState.Manual; VideoPlayerElement.Stretch = Stretch.Uniform; VideoPlayerElement.MouseWheel += mePlayer_MouseWheel; Volume = 0; //reset scrollPlayer.Content = VideoPlayerElement; double vOff = _scrollDragger.VerticalOffset; double zoom = _scrollDragger.Zoom; _scrollDragger = new ScrollDragZoom(VideoPlayerElement, scrollPlayer); _scrollDragger.Zoom = zoom; _scrollDragger.VerticalOffset = vOff; _scrollDragger.SizeChangedAction = () => { txtVideoResolution.Text = string.Format("{0:0}x{1:0} ({2:0.0}%)", VideoPlayerElement.ActualWidth, VideoPlayerElement.ActualHeight, 100.0 * _scrollDragger.Zoom); }; //refresh view when change position VideoPlayerElement.ScrubbingEnabled = true; AddFlipXRenderTransform(VideoPlayerElement, flipHorizontally); VideoPlayerElement.MediaOpened += (s, e) => { double zoom_save = _scrollDragger.Zoom; _scrollDragger.NaturalSize = new Size(VideoPlayerElement.NaturalVideoWidth, VideoPlayerElement.NaturalVideoHeight); _scrollDragger.Zoom = zoom_save; MediaState = GetMediaState(VideoPlayerElement); VideoStarted(this); }; VideoPlayerElement.MediaEnded += (s, e) => { VideoEnded(); }; VideoPlayerElement.MediaFailed += (s, e) => { e.Handled = VideoFailed(e, VideoPlayerElement); }; } catch (Exception err) { MessageBox.Show(err.ToString()); } }