WPF Mediaelement.Position无法正常工作 [英] WPF Mediaelement.Position is not working
问题描述
我需要在WPF的 MediaElement
控件上设置 Position
属性.但是,当我播放视频(通过 Play()
或通过 Opacity
上的某种动画)播放时,它根本无法工作.它显示的是00:00:00时间,但我希望将其设置为00:00:05.
What I need is to set the Position
property on WPF's MediaElement
control. But when I play the video, (either via Play()
or through some kind of animation on Opacity
) it is is not working at all. It is showing 00:00:00 time, but I would expect it to be set to 00:00:05.
我有一个硬编码的Position值,它根本不起作用.
I have hard-coded Position value and it is not working at all.
以防万一我要把所有的代码都放进去,以便您可以看到整个动画逻辑.
Just In case I am going to put all my code I have so u can see the whole animation logic.
有任何线索吗?
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();
但没有喜悦...
推荐答案
我找到了一些解决方案.这并不完全理想,因为有时会显示第一个视频帧,但至少可以正常工作.
I found some solution. It is not completely ideal because sometimes it shows the first video frame, but at least it is working.
-
我们需要可以在其中应用新的
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);
}
我们必须像这样关闭 Mediaelement
.
Player1.Stop();
Player1.Close();
Player1.Source = uri;
Player1.Play();
玩得开心!(啤酒)
这篇关于WPF Mediaelement.Position无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!