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

后端 未结 15 2181
逝去的感伤
逝去的感伤 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:18

    time = FileLength / (Sample Rate * Channels * Bits per sample /8)

    0 讨论(0)
  • 2020-11-30 01:20

    I'm gonna have to say MediaInfo, I have been using it for over a year with a audio/video encoding application I'm working on. It gives all the information for wav files along with almost every other format.

    MediaInfoDll Comes with sample C# code on how to get it working.

    0 讨论(0)
  • 2020-11-30 01:23

    I had difficulties with the example of the MediaPlayer-class above. It could take some time, before the player has opened the file. In the "real world" you have to register for the MediaOpened-event, after that has fired, the NaturalDuration is valid. In a console-app you just have to wait a few seconds after the open.

    using System;
    using System.Text;
    using System.Windows.Media;
    using System.Windows;
    
    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main(string[] args)
        {
          if (args.Length == 0)
            return;
          Console.Write(args[0] + ": ");
          MediaPlayer player = new MediaPlayer();
          Uri path = new Uri(args[0]);
          player.Open(path);
          TimeSpan maxWaitTime = TimeSpan.FromSeconds(10);
          DateTime end = DateTime.Now + maxWaitTime;
          while (DateTime.Now < end)
          {
            System.Threading.Thread.Sleep(100);
            Duration duration = player.NaturalDuration;
            if (duration.HasTimeSpan)
            {
              Console.WriteLine(duration.TimeSpan.ToString());
              break;
            }
          }
          player.Close();
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-30 01:24

    Try code below from How to determine the length of a .wav file in C#

        string path = @"c:\test.wav";
        WaveReader wr = new WaveReader(File.OpenRead(path));
        int durationInMS = wr.GetDurationInMS();
        wr.Close();
    
    0 讨论(0)
  • 2020-11-30 01:24

    You might find that the XNA library has some support for working with WAV's etc. if you are willing to go down that route. It is designed to work with C# for game programming, so might just take care of what you need.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题