Dimi Dimi - 1 month ago 14
C# Question

WPF Mediaelement.Position is not working

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.

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.

Any clue?

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();
}
}
}


I have tried to do like

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


But no joy...

Answer

I found some solution. It is not completely ideal because sometimes it shows the first video frame, but at least it is working.

  1. We need those events where we can apply new 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. We have to close Mediaelement like this.

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

Have fun! (beer)