I m using c#.net and in web based application i want to calculate the time duration of wav file in minutes. Please help me out.
Thanks

Dani AI

Generated

— two practical ways to get a WAV file duration in minutes for a web app. already pointed to external discussion; below are ready-to-use, self-contained methods (library and manual) plus key caveats.

Library (recommended): NAudio gives TotalTime directly and handles many WAV variants and compressed wrappers.

using NAudio.Wave;

public double GetWavMinutes(string path)
{
    using (var r = new WaveFileReader(path))
    {
        return r.TotalTime.TotalMinutes;
    }
}

// If you have an uploaded file stream (HttpPostedFile.InputStream or IFormFile.OpenReadStream()):
// using (var r = new WaveFileReader(stream)) { return r.TotalTime.TotalMinutes; }

Manual (no dependency): read the RIFF/WAVE header, grab the fmt chunk byteRate and the data chunk size, then compute seconds = dataSize / byteRate. This works for standard WAVs and is fast because it does not decode audio.

public static double GetWavMinutes(string path)
{
    using (var fs = File.OpenRead(path))
    using (var br = new BinaryReader(fs, System.Text.Encoding.ASCII))
    {
        if (System.Text.Encoding.ASCII.GetString(br.ReadBytes(4)) != "RIFF") throw new InvalidDataException();
        br.ReadInt32();
        if (System.Text.Encoding.ASCII.GetString(br.ReadBytes(4)) != "WAVE") throw new InvalidDataException();

        int byteRate = 0;
        int dataSize = 0;

        while (fs.Position < fs.Length)
        {
            var id = System.Text.Encoding.ASCII.GetString(br.ReadBytes(4));
            var size = br.ReadInt32();
            if (id == "fmt ")
            {
                br.ReadInt16(); // audio format
                br.ReadInt16(); // channels
                br.ReadInt32(); // sample rate
                byteRate = br.ReadInt32();
                br.ReadBytes(4); // block align + bits/sample (skip)
                if (size > 16) br.ReadBytes(size - 16);
            }
            else if (id == "data")
            {
                dataSize = size;
                break;
            }
            else
            {
                br.ReadBytes(size);
            }
            if ((size & 1) == 1) br.ReadByte();
        }

        if (byteRate <= 0 || dataSize <= 0) throw new InvalidDataException();
        return (double)dataSize / byteRate / 60.0;
    }
}

Cautions and tips: use the library if the WAV may be compressed or non-PCM. When reading uploaded files in ASP.NET, operate on the request stream (reset Position = 0 if previously read). Always dispose readers, handle malformed headers, and account for chunk padding (code above does). As observed the thread is old, but these approaches remain valid across .NET versions.

Recommended Answers

All 2 Replies

This thread is more than three months old. Perhaps start a new thread instead?


bokka * bokka = (bokka)2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.