How can I determine the length (i.e. duration) of a .wav file in C#?

后端 未结 15 2346
逝去的感伤
逝去的感伤 2020-11-30 00:26

In the uncompressed situation I know I need to read the wav header, pull out the number of channels, bits, and sample rate and work it out from there: (channels) * (bits) *

15条回答
  •  猫巷女王i
    2020-11-30 01:14

    Yes, There is a free library that can be used to get time duration of Audio file. This library also provides many more functionalities.

    TagLib

    TagLib is distributed under the GNU Lesser General Public License (LGPL) and Mozilla Public License (MPL).

    I implemented below code that returns time duration in seconds.

    using TagLib.Mpeg;
    
    public static double GetSoundLength(string FilePath)
    {
        AudioFile ObjAF = new AudioFile(FilePath);
        return ObjAF.Properties.Duration.TotalSeconds;
    }
    

提交回复
热议问题