Length in time of a wave file

You can use CSCore or NAudio:

CSCore (extracted from this sample, current playback position and total duration are used here):

using System;
using CSCore;
using CSCore.Codecs.WAV;

IWaveSource wavSource = new WaveFileReader(stream);
TimeSpan totalTime = wavSource.GetLength();

NAudio:

using System;
using NAudio.Wave;

using (var wfr = new WaveFileReader(stream))
{
    TimeSpan totalTime = wfr.TotalTime;
}

Also see the MSDN docs for TimeSpan.

The duration is calculated from the total length of the WAVE data (which can be an estimate for compressed files) and the average bytes per second (as per the NAudio source in property TotalTime):

totalTimeInSeconds = LengthInBytes / AverageBytesPerSecond;

Tags:

C#

Cscore