Akai

Hi

im using Windows Media Player to get the duration of the media file being played. However the duration i get is always 0 but i can retrieve the filename. The reason for using a console application is because i only want to get the duration of the file and not play it. I have posted part of my code as below. Can anyone help me out pls

I called this mkfunction() in my main function.

public static void mkfunction()

{

WindowsMediaPlayer m = new WindowsMediaPlayer();

m.URL = @"C:\MK_Projects\Video Clips\story_00.mpg";

m.controls.play();

string name = m.currentMedia.name;

double duration = m.currentMedia.duration;

Console.WriteLine("File Name: " + name);

Console.WriteLine("Duration: {0}", duration);

}

-------------------------------------------------------------------------------------------

Output results:

File Name: story_00
Duration: 0

Thank You!!



Re: Visual C# General C# WMP console application to get duration of current media file

Figo Fei - MSFT

Hi, Akai

The media file has not been added to the currentMedia when you called m.currentMedia.duration, It need time to load the information.

So add a delay for loading time, it will show the proper information, use the following code:

Code Snippet

public static void mkfunction()
{
WindowsMediaPlayer m = new WindowsMediaPlayer();
m.URL = @"C:\MK_Projects\Video Clips\story_00.mpg";
m.controls.play();
System.Threading.Thread.Sleep(1000);//delay time
string name = m.currentMedia.name;
string duration = m.currentMedia.durationString;
Console.WriteLine("File Name: " + name);
Console.WriteLine("Duration: {0}", duration);
}

Thanks






Re: Visual C# General C# WMP console application to get duration of current media file

Akai

Hi Figo

yup your single line of the Thread.Sleep() definitely helps me to simplify alot of my messy codes haha. Hmm but i find that for some of my mpg files, they would need a Sleep duration of at least 3000 in order to detect their duration. Im wondering if these few files have some bad sectors within them.

I have yet another question to ask. I uses the GetFiles function to find all files within a specified directory. Below is part of my code:

DirectoryInfo d = new DirectoryInfo(@"C:\MK_Projects\Video Clips");

//Get all files for the current directory

files = d.GetFiles("*.*");

Is it possible if i want to search for files with only mpg and wmv extensions, pls advise me.

Thank You





Re: Visual C# General C# WMP console application to get duration of current media file

Figo Fei - MSFT

Hi, Akai

For one extension filter simply use GetFiles to get files end with .mpg:

Code Snippet

DirectoryInfo d = new DirectoryInfo(@"C:\MK_Projects\Video Clips");

//Get all files for the current directory

FileInfo[] files = d.GetFiles("*.mpg");

foreach (FileInfo file in files)
{
Console.WriteLine(file.FullName);
}

For multiple extension filters, utilize the method EndsWith to detect file names:

Code Snippet
DirectoryInfo d = new DirectoryInfo(@"C:\MK_Projects\Video Clips");
FileInfo[] files = d.GetFiles("*.*");
foreach (FileInfo file in files)
{
if (file.Name.EndsWith(".mpg") || file.Name.EndsWith(".wmv"))
{
Console.WriteLine(file.FullName);
}
}

Thanks






Re: Visual C# General C# WMP console application to get duration of current media file

Akai

Hi

thxs for your advise... the method for detecting multiple filenames works great for me.

Thank You





Re: Visual C# General C# WMP console application to get duration of current media file

Figo Fei - MSFT

You're welcome Smile




Re: Visual C# General C# WMP console application to get duration of current media file

Testing 1 2 3

Hi what is the name of the system file that needs to be included in order to use WindowsMediaPlayer m = new WindowsMediaPlayer()



Re: Visual C# General C# WMP console application to get duration of current media file

Figo Fei - MSFT

We should first add reference to WMPLib which will be found if you install windows media player (PIA) to interop with .NET

For more information, see: http://msdn2.microsoft.com/en-us/library/bb262657.aspx

Thanks






Re: Visual C# General C# WMP console application to get duration of current media file

Testing 1 2 3

Hi I was wondering if you knew of a way to get the duration of an swf file or an mov file




Re: Visual C# General C# WMP console application to get duration of current media file

Figo Fei - MSFT

I don't think windows media player can play. So you could try to find other components for help.

Thanks