1
Vote

System.OverflowException : TimeSpan overflowed in MediaPlayer.OnVirtualPositionChanged()

description

Hi folks we are having problems here were our app is occasionally hitting this stowed exception:
System.OverflowException: TimeSpan overflowed because the duration is too long.
   at System.TimeSpan.Subtract(TimeSpan ts)
   at Microsoft.PlayerFramework.MediaPlayer.OnVirtualPositionChanged(RoutedPropertyChangedEventArgs`1 e)
   at Microsoft.PlayerFramework.MediaPlayer.OnUpdate()
   at Microsoft.PlayerFramework.MediaPlayer.UpdateTimer_Tick(Object sender, Object e)', Sender: '<null>'. Missing try/catch blocks.
The problem seems to derive from
        /// <summary>
        /// Initializes the MediaPlayer once the media has opened but immediately before the MediaOpened event fires.
        /// </summary>
        protected virtual void OnMediaOpened()
        {
            ...
            SetValue(NaturalDurationProperty, _NaturalDuration);
The getter for _NaturalDuration looks like this:
        Duration _NaturalDuration
        {
            get { return MediaElementElement != null ? MediaElementElement.NaturalDuration : DefaultNaturalDuration; }
        }
However according to the comment above OnMediaOpened(), the MediaOpened event has not yet been fired and according to the MediaElement documentation: "NaturalDuration is not accurate until the MediaOpened event has been raised."
And indeed the problem derives from the fact that also according to the MediaElement docs, "Live media streams report a NaturalDuration of Int64.MaxValue" but occasionally (and more commonly on redstone than on rs2) we find that it is MinValue which leads to an the exception as OnNaturalDurationChanged sets EndTime to this value and then OnVirtualPositionChanged attempts to subtract the virtual position from it.

The fix that works for me is to simply detect MinValue and avoid it like so:
        Duration _NaturalDuration
        {
            get {
                if (MediaElementElement != null && MediaElementElement.NaturalDuration == TimeSpan.MinValue)
                {
                    // it's hack time!
                    return DefaultNaturalDuration;
                }
                else
                {
                    // do the normal thing
                    return MediaElementElement != null ? MediaElementElement.NaturalDuration : DefaultNaturalDuration;
                }
            }
        }
But i'm loathe to submit this until I have a better understanding of what's going on. Is this a bug in Player Framework or in the Windows platform, or is there something we are doing to cause this?

Hoping to find someone who knows more about this stuff than I.

comments