MediaElement.NaturalDuration throws an exception when requesting TimeSpan.TotalSeconds

I have a winforms form in which I use MediaElement. Everything works (more or less) perfectly. The only thing, after loading the media file (mp3) accessing NaturalDuration.TimeSpan.TotalSeconds, throws an exception

InvalidOperatoinException: The TimeSpan property cannot be returned for the automatic duration value.

(Translation from German itself)

How can I access the timepan resource? There are no special settings used in all the examples I found (ok, they seem to be native WPF ...)

I am using VisualStudio Express.Net 4, C # on Windows 7.

Any clues?

+3
source share
1 answer

You need to wait for the event MediaOpenedto start, it NaturalDurationwill be available after that. To check if a value is available, you can use the property NaturalDuration.HasTimeSpan.

+10
source

Source: https://habr.com/ru/post/1772448/


All Articles