Menu
26 Oct 2009CPOL
If you have ever looked at a media file (audio or video) in explorer on a Windows 7 PC, you may have noticed that it displays additional information about that media file that previous versions of Windows didn't seem to have access to, for example the length/duration of a Quicktime Movie Clip.
This article shows how to use Windows Media Player control in a WPF application to avoid the performance degradation of MediaElement; Author: Kashif_Imran.
How do I get Windows Media Player to work. Faulting application start time: 0x01d34865f5e9a680 Faulting application path: C: Program Files (x86). Player.currentMedia.duration and duration string property for a timer or Thread.sleep application but the value returned by both is always 0. I also tried giving a delay of 1-2 seconds before calling the duration property but this doesn't work as some of my audio tracks are just 2 seconds long.
If you have ever looked at a media file (audio or video) in the explorer window on a Windows 7 PC, you may have noticed that it displays additional information about that media file that previous versions of Windows didn't seem to have access to, for example the length/duration of a Quicktime Movie Clip:
Even right clicking the file and choosing Properties > Details does not give me this information on my Vista Ultimate PC. Of course, now that Windows has the ability to fetch this information, so do we as developers, through the Windows API (The DLL to Import by the way is 'propsys.dll'):
However, before you rush off to play with these, you may be interested to know that Microsoft has created a great Library that showcases this and many of the other new API features of Windows 7. It's called the
WindowsAPICodePack
and you can get it here.If you open the
WindowsAPICodePack
Solution and compile the Shell Project, it creates a nice wrapper around all the neat new system properties available through propsys.dll. Adding a reference to WindowsAPICodePack.dll and WindowsAPICodePack.Shell.dll to a console application will allow you to get the duration of just about any media file that Windows recognizes. (Of course the more codec packs you install, the more types it will recognize, I recommend The Combined Community Codec Pack to maximize your range of playable files.)Here is a simple example showing how to get the duration of a media file in C# using this library:
As you can see, the
System.Media.Duration
Property returns a value in 100ns units so some simple math will turn it into seconds. Download the Test Project which includes the prebuilt WindowsAPICodePack.dll and WindowsAPICodePack.Shell.dll files in the bin folder: C# Media Player Example
For the curious, I tested this on Windows XP and as you'd expect, it didn't work:
On Vista Ultimate SP2, it still didn't work - nanoseconds was always 0, though it didn't throw any exceptions.
For the older systems, I guess we are limited to using the old MCI (Media Control Interface) API:
Which works fine for .mp3 and .avi and other formats that play natively in Windows Media Player, but even with a codec pack installed, it doesn't work on Quicktime or .mp4 files, where the new Windows 7 API did.
Active6 months ago
I am using Windows Media Player object for playing video in my C# winforms project.
My test.avi duration is 12 seconds. I would like to play that between 4 and 8 seconds.
I can start video from 4 second as below;
So how can i stop the video 8th seconds after playing video?
Thorkil Holm-JacobsenC Media Player Duration Of Playlist
4,39333 gold badges1919 silver badges3737 bronze badges
KerberosKerberos69755 gold badges1818 silver badges3939 bronze badges
1 Answer
Alex JoligAlex Jolig10k1616 gold badges9191 silver badges124124 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.