如何确定C#中.wav文件的长度(即持续时间)?

Joh*_*bly 27 c# compression audio

在未压缩的情况下,我知道我需要读取wav标头,拉出通道数,位数和采样率并从那里开始工作:(通道)*(位)*(样本/ s)*(秒) =(filesize)

有没有更简单的方法 - 一个免费的库,或者.net框架中的某些东西?

如果压缩.wav文件(例如使用mpeg编解码器),我该怎么做?

小智 35

http://naudio.codeplex.com/链接下载NAudio.dll

然后使用此功能

public static TimeSpan GetWavFileDuration(string fileName)       
{     
    WaveFileReader wf = new WaveFileReader(fileName);
    return wf.TotalTime; 
}
Run Code Online (Sandbox Code Playgroud)

你会得到持续时间


Jan*_*ich 20

您可以考虑使用mciSendString(...)函数(为清晰起见,省略了错误检查):

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Sound
{
    public static class SoundInfo
    {
        [DllImport("winmm.dll")]
        private static extern uint mciSendString(
            string command,
            StringBuilder returnValue,
            int returnLength,
            IntPtr winHandle);

        public static int GetSoundLength(string fileName)
        {
            StringBuilder lengthBuf = new StringBuilder(32);

            mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", fileName), null, 0, IntPtr.Zero);
            mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
            mciSendString("close wave", null, 0, IntPtr.Zero);

            int length = 0;
            int.TryParse(lengthBuf.ToString(), out length);

            return length;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Man*_*yak 7

是的,有一个免费的库可用于获取音频文件的持续时间。该库还提供了更多功能。

标签库

TagLib 根据 GNU 宽通用公共许可证 (LGPL) 和 Mozilla 公共许可证 (MPL) 分发。

我实现了下面的代码,它返回持续时间(以秒为单位)。

using TagLib.Mpeg;

public static double GetSoundLength(string FilePath)
{
    AudioFile ObjAF = new AudioFile(FilePath);
    return ObjAF.Properties.Duration.TotalSeconds;
}
Run Code Online (Sandbox Code Playgroud)


Lar*_*ars 5

我在上面的 MediaPlayer 类示例中遇到了困难。在播放器打开文件之前可能需要一些时间。在“现实世界”中,您必须注册 MediaOpened 事件,在触发之后,NaturalDuration 是有效的。在控制台应用程序中,您只需在打开后等待几秒钟。

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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Jos*_*ola 5

不要接受已经接受的答案,但我能够使用Microsoft.DirectX.AudioVideoPlayBack命名空间获得音频文件的持续时间(几种不同的格式,包括AC3,这是我当时需要的) .这是DirectX 9.0 for Managed Code的一部分.添加对它的引用使我的代码像这样简单......

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
Run Code Online (Sandbox Code Playgroud)

而且它也很快!这是Audio类的参考.