Aurigma Graphics Mill 5.5 for .NET
Determining Audio/Video File Length
If you need to determine the length of some audio or video file, you can do it in two ways:
- Read the MediaProcessorMetadataDictionary.Length field. This will work only if the reader supports metadata extraction and the file contains that field. For more details about reading ID3 fields, see the Reading Metadata topic.
- Read the DSReader.Duration property (or QTReader.Duration or WMReader.Duration—the choice depends on the reader you use).
As metadata are not always present in the file, the second approach is used in this example.
The function that will determine the duration of the file should perform the following operations:
- Create a reader and open the source file.
- Read the appropriate Duration property depending on the reader used.
- Close the reader.
Visual Basic
'Returns the value measured in 1/100s of a second
Public Function GetLength(ByVal inputFilename As String) As Int32
Dim duration As Int32
Try
'Create the reader
Dim reader As IFormatReader = MediaFormatManager.CreateFormatReader _
(inputFilename)
'Get length from the reader
If TypeOf reader Is QTReader Then
Dim _reader As QTReader = CType(reader, QTReader)
duration = _reader.Duration
ElseIf TypeOf reader Is WMReader Then
Dim _reader As WMReader = CType(reader, WMReader)
duration = _reader.Duration
Else
Dim _reader As DSReader = CType(reader, DSReader)
duration = _reader.Duration
End If
reader.Dispose()
Return duration
Catch ex As Exception
Console.WriteLine(ex)
Return duration
End Try
End Function
C#
//Returns the value measured in 1/100s of a second
public static int GetLength(string inputFilename)
{
int duration = -1;
try
{
//Create the reader
IFormatReader reader = MediaFormatManager.CreateFormatReader(inputFilename);
//Get length from the reader
if (reader.GetType().Equals(typeof(QTReader)))
{
QTReader _reader = (QTReader)reader;
duration = _reader.Duration;
}
else if (reader.GetType().Equals(typeof(WMReader)))
{
WMReader _reader = (WMReader)reader;
duration = _reader.Duration;
}
else
{
DSReader _reader = (DSReader)reader;
duration = _reader.Duration;
}
reader.Dispose();
return duration;
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
return duration;
}
}