PlaybackPositionChanged event

Topics: Windows Phone 8
Apr 12, 2013 at 9:19 PM
Edited Apr 12, 2013 at 9:20 PM
Dear all,
I upgraded the player from Windows Phone 7 to WP8 and a lot of things are changed. In the WP7 version the_PlaybackPositionChanged event make me able to hold the last frame of the video when it was close the end preventing video disappearing. Now in this version I didn't find anything similar, I need to hold the last frame of the video. I had to upgrade the version becuase the old version does not work on Windows Phone 8.

Many thanks
Armando
Coordinator
Apr 12, 2013 at 10:51 PM
Hello, regarding your question about the Position changed event: there is an event called PositionChanged that offers the equivalent to PlaybackPositionChanged in WP7.

There is a lot that has changed from the WP7 version and I feel your pain if you are updating your app and weren't expecting this. However, there are good reasons for each difference. In a nutshell, we built the WP8 version based on the Win8 version and the Win8 version was re-architected to work with the new Media Foundation pipeline and support code sharing with JS apps via WinRT libraries. With this came a number of architectural improvements (e.g. decoupling reusable features from the MediaElement and smooth streaming). Regarding Position and PositionChanged: the Win8 (and WP8) versions are intended to be backward compatible with MediaElement and MediaElement uses Position instead of PlaybackPosition. To follow with the recommended convention, the event was renamed to match.

Let me know if you have any other questions about the differences, Tim
Apr 16, 2013 at 9:21 PM
Hi Tim,
thanks a lot for your reply.

I have still problems collecting current stream position. How I can get the video position inside PositionChanged event?
OldValue and New Value are always 0 and the event is also raised even when video is ended.
In the old version I used this code to hlod the video on last frame

private void m_Player_PlaybackPositionChanged(object sender, Microsoft.SMF.Core.CustomEventArgs<TimeSpan> e)
{
TimeSpan currentPosition = m_Player.PlaybackPosition + TimeSpan.FromMilliseconds(100);
if (currentPosition > m_Player.EndPosition)
   m_Player.Pause();
}

I changed EndPosition with Duration but
now PlaybackPosition parameter is disappeared, what can I use as substitution?

Thanks again for your help
Armando
Coordinator
Apr 22, 2013 at 7:29 PM
Hi Armando, I was unable to reproduce the issue where NewValue & OldValue always equal 0. I wonder if it could somehow be specific to your stream? If you could send a repro, I'd be happy to investigate further.

But, once you have this working, you should be able to do the same thing you were doing before but with the newer APIs. For example:
player.PositionChanged += player_PositionChanged;

void player_PositionChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<TimeSpan> e)
{
    TimeSpan currentPosition = e.NewValue + TimeSpan.FromMilliseconds(100);
    if (currentPosition > player.EndTime)
        player.Pause();
}
May 1, 2013 at 4:18 PM
Hi Tim,
I am really appreciating your help about this issue.
I created a sample to let understand you the problem.
Download it from https://dl.dropboxusercontent.com/u/28844909/PlayerTestWP8.rar

I applied the code as you suggests, but it still happens that most of the time NewValue is zero (sometime it returned a value and so the player paused but the video was ended yet) and I noticed a strange behaviour PositionChanged event raises also when the video is ended.

All the video that I am playing are very short, probably the longest one is 3 sec, but the average is around 1 sec.

Demo app has two buttons, click on Load Video to download the video from Internet the first time.

Thanks for your time.

Armando
Coordinator
May 17, 2013 at 6:14 PM
Hi Armando, I was just wondering if you have considered doing this instead:
 m_Player.MediaEnded += m_Player_MediaEnded;

void m_Player_MediaEnded(object sender, Microsoft.PlayerFramework.MediaPlayerActionEventArgs e)
{
    m_Player.Position = m_Player.EndTime;
}
There's a very slight flicker at the end as it jumps to the beginning for a split second before jumping back to the end but it appears to accomplish what you want.
Coordinator
May 17, 2013 at 6:18 PM
One more thing... you can also set MediaPlayer.UpdateInterval to a timespan. This determines how often the position is updated. By default this is set to 250 ms which might be too high for your 1 second videos. Just note: the smaller the update interval, the greater the performance penalty so be sure to test well on lower end devices if setting this manually.