如何确定在C#中的.wav文件的长度? [英] How can I determine the length of a .wav file in C#?

查看:479
本文介绍了如何确定在C#中的.wav文件的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我知道我需要阅读WAV头,拔出渠道的位数和采样率,并从那里出来的uncom pressed情况:
(信道)*(比特)*(样本/秒)*(秒)=(文件大小)

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) * (samples/s) * (seconds) = (filesize)

有没有更简单的方式 - 一个免费的图书馆,或者说在.NET Framework也许

Is there a simpler way - a free library, or something in the .net framework perhaps?

我将如何做到这一点,如果.wav文件为com pressed(与MPEG codeC为例)?

How would I do this if the .wav file is compressed (with the mpeg codec for example)?

推荐答案

您可以考虑使用mciSendString(...)(省略为清楚起见错误检查)功能:

You may consider using the mciSendString(...) function (error checking is omitted for clarity):

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;
        }
    }
}

这篇关于如何确定在C#中的.wav文件的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆