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

后端 未结 15 2299
逝去的感伤
逝去的感伤 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条回答
  •  一生所求
    2020-11-30 01:25

    Not to take anything away from the answer already accepted, but I was able to get the duration of an audio file (several different formats, including AC3, which is what I needed at the time) using the Microsoft.DirectX.AudioVideoPlayBack namespace. This is part of DirectX 9.0 for Managed Code. Adding a reference to that made my code as simple as this...

    Public Shared Function GetDuration(ByVal Path As String) As Integer
        If File.Exists(Path) Then
            Return CInt(New Audio(Path, False).Duration)
        Else
            Throw New FileNotFoundException("Audio File Not Found: " & Path)
        End If
    End Function
    

    And it's pretty fast, too! Here's a reference for the Audio class.

提交回复
热议问题