1

I'm trying to get the duration for an audio file in c#. I got the following code to work on my local machine but when I deploy it to a windows server 2008 box it always returns a duration of 0 for .avi and .wav files.

WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
    IWMPMedia mediaInfo = wmp.newMedia(filePath);
    return mediaInfo.durationString

I have windows media player 11 installed on the server.

Any help would be most appreciated

M4N
  • 94,805
  • 45
  • 217
  • 260

3 Answers3

2

I don't have experience with "WindowsMediaPlayerClass", but I have used TagLibSharp extensively, which is what I try to use for such things - it supports a pretty broad set of formats and that makes it's API much more usable that the built-in support in .NET

http://developer.novell.com/wiki/index.php/TagLib_Sharp

Quite possibly simply using TagLibSharp will solve your problems.

(Note that taglibsharp is maintained by the banshee project, but is hosted @ novell for apparently historical reasons. I don't know about the details, but that can make contacting the devs a little confusing)

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
2

To retrieve duration info, media file must be opened first.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd564790(v=vs.85).aspx#Y656

To retrieve the duration for files that are not in the user's library, you must wait for Windows Media Player to open the file; that is, the current OpenState must equal MediaOpen. You can verify this by handling the AxWindowsMediaPlayer._WMPOCXEvents_OpenStateChange event or by periodically checking the value of AxWindowsMediaPlayer.openState.

The link also has sample codes for C#.

If you don't like to play the video, you may use the IWMPMedia class:

http://johndyer.name/post/Retreiving-the-duration-of-a-WMV-in-C.aspx

kenankule
  • 21
  • 3
1

My guess is that the media player isn't installed (it's not by default on server SKUs).

Did you try installing the desktop experience package?

Larry Osterman
  • 16,086
  • 32
  • 60
  • This is completely unrelated to your answer here, but I was hoping to draw your attention to this question I asked a couple of days ago: http://stackoverflow.com/questions/1303811/what-is-the-latency-or-delay-time-for-callbacks-from-the-waveoutwrite-api-metho – MusiGenesis Aug 23 '09 at 16:36