MediaElement冻结了视频

时间:2012-12-14 13:12:21

标签: c# wpf

我将一些LinearGradientBrush动画应用于MediaElement并且在此视频冻结之后。

我尝试通过Player1.OpacityMask = null;重置它,但没有快乐。

顺便说一下,如果我为Opacity的{​​{1}}制作动画,那么视频就会显示为正常。

任何线索是什么?将MediaElement动画应用到LinearGradientBrush后,如何修复视频冻结?

谢谢!

MediaElement

更新:

应用 shader 效果后,会出现同样的问题。所以视频在它之前很顺利,之后就是跳跃/冻结。似乎我们以某种方式重置 /// <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(); } } 或?

MedieElement

0 个答案:

没有答案