kungfufrog
kungfufrog

Reputation:

Getting duration of audio file C#

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

Upvotes: 1

Views: 6938

Answers (3)

kenankule
kenankule

Reputation: 21

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

Upvotes: 2

Larry Osterman
Larry Osterman

Reputation: 16142

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?

Upvotes: 1

Eamon Nerbonne
Eamon Nerbonne

Reputation: 48136

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)

Upvotes: 2

Related Questions