MediaElement.NaturalDuration throws exception when querying TimeSpan.TotalSeconds

时光怂恿深爱的人放手 提交于 2019-12-07 08:06:07

问题


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

InvalidOperatoinException: A TimeSpan property can not be returned for a duration value of automatic.

(Translated from german by myself)

How can I access the timespan property? There is no special setting used in all the examples I have found (ok, they seem to be WPF native...)

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

Any hints?


回答1:


You need to wait for MediaOpened event to fire, NaturalDuration will be available after that. To check if value is available, you can use NaturalDuration.HasTimeSpan property.



来源:https://stackoverflow.com/questions/4068332/mediaelement-naturalduration-throws-exception-when-querying-timespan-totalsecond

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!