MediaElement freezes video

I apply the LinearGradientBrush animation to the MediaElement and after that freeze the video.

I am trying to reset through Player1.OpacityMask = null; but not joy.

By the way, if I animate the Opacity MediaElement , then the video is displayed as usual.

Any question what? How to fix video freeze after applying LinearGradientBrush animation before MediaElement ?

Thanks!

  /// <summary> /// Interaction logic for WipeTransition.xaml /// </summary> public partial class WipeTransition { public WipeDirections Direction { set; get; } public TimeSpan Duration = TimeSpan.FromSeconds(2); public bool StopPrevPlayerOnAnimationStarts; Random rdm = new Random(1); bool isPlayer1; public WipeTransition() { InitializeComponent(); try { Loaded += WipeTransition_Loaded; SizeChanged += WipeTransition_SizeChanged; Player1.CacheMode = new BitmapCache(); Player1.LoadedBehavior = MediaState.Manual; Player2.CacheMode = new BitmapCache(); Player2.LoadedBehavior = MediaState.Manual; Direction = WipeDirections.RandomDirection; Player1.Width = ActualWidth; Player2.Width = ActualWidth; Player1.Height = ActualHeight; Player2.Height = ActualHeight; isPlayer1 = true; } catch (Exception) { } } void WipeTransition_SizeChanged(object sender, SizeChangedEventArgs e) { Player1.Width = ActualWidth; Player2.Width = ActualWidth; Player1.Height = ActualHeight; Player2.Height = ActualHeight; } void WipeTransition_Loaded(object sender, RoutedEventArgs e) { Player1.Width = ActualWidth; Player2.Width = ActualWidth; Player1.Height = ActualHeight; Player2.Height = ActualHeight; } public void Start(Uri uri) { try { Debug.WriteLine("************** START ANIMATION ***************************************"); Player1.Width = ActualWidth; Player2.Width = ActualWidth; Player1.Height = ActualHeight; Player2.Height = ActualHeight; // We start to Animate one of the Player is on BACK if (isPlayer1) { if (StopPrevPlayerOnAnimationStarts) Player2.Stop(); Canvas.SetZIndex(Player2, 49); Canvas.SetZIndex(Player1, 50); Player1.Source = uri; Debug.WriteLine("Player1 - ANIMATE"); WipeAnimation(Player1); } else // Player2 { if (StopPrevPlayerOnAnimationStarts) Player1.Stop(); Canvas.SetZIndex(Player1, 49); Canvas.SetZIndex(Player2, 50); Player2.Source = uri; Debug.WriteLine("Player2 - ANIMATE"); WipeAnimation(Player2); } } catch (Exception) { } } public void WipeAnimation(FrameworkElement ObjectToAnimate) { try { LinearGradientBrush OpacityBrush = new LinearGradientBrush(); #region Setup Wipe Direction switch (Direction) { case WipeDirections.RightToLeft: OpacityBrush.StartPoint = new Point(1, 0); OpacityBrush.EndPoint = new Point(0, 0); break; case WipeDirections.LeftToRight: OpacityBrush.StartPoint = new Point(0, 0); OpacityBrush.EndPoint = new Point(1, 0); break; case WipeDirections.BottomToTop: OpacityBrush.StartPoint = new Point(0, 1); OpacityBrush.EndPoint = new Point(0, 0); break; case WipeDirections.TopToBottom: OpacityBrush.StartPoint = new Point(0, 0); OpacityBrush.EndPoint = new Point(0, 1); break; case WipeDirections.RandomDirection: #region int randomValue = rdm.Next(0, 3); if (randomValue == 0) { OpacityBrush.StartPoint = new Point(1, 0); OpacityBrush.EndPoint = new Point(0, 0); } else if (randomValue == 1) { OpacityBrush.StartPoint = new Point(0, 0); OpacityBrush.EndPoint = new Point(0, 1); } else if (randomValue == 2) { OpacityBrush.StartPoint = new Point(0, 0); OpacityBrush.EndPoint = new Point(1, 0); } else if (randomValue == 3) { OpacityBrush.StartPoint = new Point(0, 1); OpacityBrush.EndPoint = new Point(0, 0); } #endregion break; } #endregion GradientStop BlackStop = new GradientStop(Colors.Black, 0); GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0); if (FindName("TransparentStop") != null) UnregisterName("TransparentStop"); RegisterName("TransparentStop", TransparentStop); if (FindName("BlackStop") != null) UnregisterName("BlackStop"); this.RegisterName("BlackStop", BlackStop); OpacityBrush.GradientStops.Add(BlackStop); OpacityBrush.GradientStops.Add(TransparentStop); ObjectToAnimate.OpacityMask = OpacityBrush; Storyboard sb = new Storyboard(); DoubleAnimation DA = new DoubleAnimation() { From = 0, To = 1, Duration = Duration }; DoubleAnimation DA2 = new DoubleAnimation() { From = 0, To = 1, Duration = Duration }; sb.Children.Add(DA); sb.Children.Add(DA2); Storyboard.SetTargetName(DA, "TransparentStop"); Storyboard.SetTargetName(DA2, "BlackStop"); Storyboard.SetTargetProperty(DA, new PropertyPath("Offset")); Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset")); sb.Duration = Duration; sb.Completed += sb_Completed; sb.Begin(this); if (isPlayer1) { Player1.Play(); } else // Player2 { Player2.Play(); } } catch (Exception) { } } void sb_Completed(object sender, EventArgs e) { if (isPlayer1) { Player1.OpacityMask = null; isPlayer1 = false; Player2.Stop(); Player2.Source = null; } else // Player2 { Player2.OpacityMask = null; isPlayer1 = true; Player1.Stop(); Player1.Source = null; } Debug.WriteLine("************** END ANIMATION ***************************************"); } public void Stop() { if (Player1.IsEnabled) Player1.Stop(); if (Player2.IsEnabled) Player2.Stop(); } } 

UPDATE:

The same problem occurs after applying the shader effect. So the video goes well, and after that it jumps / freezes. It seems that we have somehow reset MedieElement or?

  private void Button_Click_1(object sender, RoutedEventArgs e) { TransitionEffect[] effectGroup = transitionEffects[1]; TransitionEffect effect = effectGroup[1]; DoubleAnimation da = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(2.0)), FillBehavior.HoldEnd); da.AccelerationRatio = 0.5; da.DecelerationRatio = 0.5; da.Completed += da_Completed; effect.BeginAnimation(TransitionEffect.ProgressProperty, da); VisualBrush vb = new VisualBrush(this.Player1); vb.Viewbox = new Rect(0, 0, this.Player1.ActualWidth, this.Player1.ActualHeight); vb.ViewboxUnits = BrushMappingMode.Absolute; effect.OldImage = vb; this.Player1.Effect = effect; } 
+2
source share

All Articles