gpt4 book ai didi

c# - WPF Mediaelement.Position 不工作

转载 作者:行者123 更新时间:2023-11-30 15:00:54 24 4
gpt4 key购买 nike

我需要在 WPF 的 MediaElement 控件上设置 Position 属性。但是当我播放视频时(通过 Play() 或通过 Opacity 上的某种动画)它根本不起作用。它显示 00:00:00 时间,但我希望它设置为 00:00:05。

我有硬编码的位置值,但它根本不起作用。

以防万一,我要把我所有的代码都放在一起,这样你就可以看到整个动画逻辑。

有什么线索吗?

  public partial class CrossFadeTransition : UserControl
{
DoubleAnimation _doubleAnimationFrontPlayer = new DoubleAnimation();
DoubleAnimation _doubleAnimationBackPlayer = new DoubleAnimation();

Storyboard _sb1 = new Storyboard();
Storyboard _sb2 = new Storyboard();

public TimeSpan Duration = TimeSpan.FromSeconds(2);
public Dictionary<string, Position2D> PlayerPosition { set; get; }

public CrossFadeTransition()
{
InitializeComponent();

Player1.LoadedBehavior = MediaState.Manual;
Player1.UnloadedBehavior = MediaState.Stop;

Player2.LoadedBehavior = MediaState.Manual;
Player2.UnloadedBehavior = MediaState.Stop;

PlayerPosition = new Dictionary<string, Position2D>();

PlayerPosition.Add("Player1", Position2D.Front);
PlayerPosition.Add("Player2", Position2D.Back);

}
Position2D positionPlayer1;
Position2D positionPlayer2;

public void Stop()
{
if (Player1.IsEnabled)
Player1.Stop();

if (Player2.IsEnabled)
Player2.Stop();

}

public void Start(Uri uri, int? position)
{
try
{
positionPlayer1 = PlayerPosition["Player1"];

positionPlayer2 = PlayerPosition["Player2"];

if (positionPlayer1 == Library.Position2D.Back)
{
Player1.Source = uri;
if (Player1.IsEnabled)
Player1.Stop();

Player1.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!

Player1.Play();

}

if (positionPlayer2 == Library.Position2D.Back)
{
Player2.Source = uri;
if (Player2.IsEnabled)
Player2.Stop();

Player2.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!

Player2.Play();

}



_sb1.Children.Clear();
_sb2.Children.Clear();



if (positionPlayer1 == Position2D.Front)
{
_doubleAnimationFrontPlayer.From = 1;
_doubleAnimationFrontPlayer.To = 0;
_doubleAnimationFrontPlayer.Duration = new Duration(Duration);

PlayerPosition["Player1"] = Position2D.Back;
}
else if (positionPlayer1 == Position2D.Back)
{
_doubleAnimationFrontPlayer.From = 0;
_doubleAnimationFrontPlayer.To = 1;
_doubleAnimationFrontPlayer.Duration = new Duration(Duration);

PlayerPosition["Player1"] = Position2D.Front;
}

if (positionPlayer2 == Position2D.Front)
{
_doubleAnimationBackPlayer.From = 1;
_doubleAnimationBackPlayer.To = 0;
_doubleAnimationBackPlayer.Duration = new Duration(Duration);

PlayerPosition["Player2"] = Position2D.Back;
}
else if (positionPlayer2 == Position2D.Back)
{
_doubleAnimationBackPlayer.From = 0;
_doubleAnimationBackPlayer.To = 1;
_doubleAnimationBackPlayer.Duration = new Duration(Duration);

PlayerPosition["Player2"] = Position2D.Front;
}

_sb1.Children.Add(_doubleAnimationFrontPlayer);
Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer, new PropertyPath("(Panel.Opacity)"));
Storyboard.SetTarget(_doubleAnimationFrontPlayer, Player1);
_sb1.Completed += _sb1_Completed;
_sb1.Begin();

//
_sb2.Children.Add(_doubleAnimationBackPlayer);
Storyboard.SetTargetProperty(_doubleAnimationBackPlayer, new PropertyPath("(Panel.Opacity)"));
Storyboard.SetTarget(_doubleAnimationBackPlayer, Player2);
_sb2.Completed += _sb2_Completed;
_sb2.Begin();

}
catch (Exception)
{

throw;
}
}

void _sb2_Completed(object sender, EventArgs e)
{
_sb2.Completed -= _sb2_Completed;

Debug.WriteLine("Player2 COMPLETED " + DateTime.Now.TimeOfDay);

if (positionPlayer2 == Position2D.Front)
{
Player2.Stop();
}
}

void _sb1_Completed(object sender, EventArgs e)
{
_sb1.Completed -= _sb1_Completed;

Debug.WriteLine("Player1 COMPLETED " + DateTime.Now.TimeOfDay);
if (positionPlayer1 == Position2D.Front)
{
Player1.Stop();
}
}
}

我已经试过了

Player2.Play();
Player2.Pause();
Player2.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!
Player2.Play();

但没有快乐......

最佳答案

我找到了一些解决方案。它并不完全理想,因为有时它会显示第一个视频帧,但至少它可以正常工作。

  1. 我们需要那些可以应用新的 Position 的事件。

    void Player2_MediaOpened(object sender, RoutedEventArgs e)
    {
    Player2.Position = new TimeSpan(0, 0, 7);
    }

    void Player1_MediaOpened(object sender, RoutedEventArgs e)
    {
    Player1.Position = new TimeSpan(0, 0, 7);
    }
  2. 我们必须像这样关闭 Mediaelement

    Player1.Stop();
    Player1.Close();
    Player1.Source = uri;
    Player1.Play();

玩得开心! (啤酒)

关于c# - WPF Mediaelement.Position 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15027746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com