C# NAudio分割wav文件产生0秒的wav文件

C# NAudio分割wav文件产生0秒的wav文件,c#,.net,file,wav,naudio,C#,.net,File,Wav,Naudio,我正在使用NAudio将完整的专辑分割成几个音频文件。程序运行平稳,没有异常,但最终,输出的音频文件无法收听,显示为0秒 这是我的密码;albumPath描述存储单个文件的文件夹的路径,filePath是输入文件的路径 private void splitWavPrep(string albumPath,string filePath) { MediaFoundationReader reader = new MediaFoundationReader(

我正在使用NAudio将完整的专辑分割成几个音频文件。程序运行平稳,没有异常,但最终,输出的音频文件无法收听,显示为0秒

这是我的密码;albumPath描述存储单个文件的文件夹的路径,filePath是输入文件的路径

        private void splitWavPrep(string albumPath,string filePath)
    {

        MediaFoundationReader reader = new MediaFoundationReader(filePath);
        int bytesPerMilliSecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
        for (int i = 0; i < tracks.Count; i++)
        {
            string trackName = tracks[i].Name;
            int startMilliSeconds = (int) tracks[i].StartSeconds * 1000;
            int duration;
            if (i< tracks.Count - 1)
            {
                //if there's a next track
                duration = (int)(tracks[i + 1].StartSeconds*1000) - startMilliSeconds;
            }
            else
            {
                //this is the last track
                duration = (int) reader.TotalTime.TotalMilliseconds - startMilliSeconds;
            }
            int startPos = startMilliSeconds * bytesPerMilliSecond;
            startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
            int endMilliSeconds = startMilliSeconds + duration;
            int endBytes = (endMilliSeconds - startMilliSeconds) * bytesPerMilliSecond;
            endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;

            int endPos = startPos + endBytes;

            string trackPath = Path.Combine(albumPath, trackName + ".wav");
            splitWav(trackPath, startPos, endPos, reader);

        }
    }

    private async void splitWav(string trackPath, int startPos, int endPos, MediaFoundationReader reader)
    {

        int progress = 0;
        WaveFileWriter writer = new WaveFileWriter(trackPath, reader.WaveFormat); 
        reader.Position = startPos;
        byte[] buffer = new byte[1024];
        while (reader.Position < endPos)
        {
            int bytesRequired = (int)(endPos - reader.Position);
            if (bytesRequired > 0)
            {
                int bytesToRead = Math.Min(bytesRequired, buffer.Length);
                int bytesRead = reader.Read(buffer, 0, bytesToRead);
                if (bytesRead > 0)
                {
                    await writer.WriteAsync(buffer, 0, bytesRead);
                    progress += bytesRead;
                }
            }
        }
    }
private void splitWavPrep(字符串相册路径、字符串文件路径)
{
MediaFoundationReader=新的MediaFoundationReader(文件路径);
int bytespermillissecond=reader.WaveFormat.AverageBytesPerSecond/1000;
对于(int i=0;i0)
{
int bytesToRead=Math.Min(bytesRequired,buffer.Length);
int bytesRead=reader.Read(缓冲区,0,bytesToRead);
如果(字节读取>0)
{
wait writer.WriteAsync(缓冲区,0,字节读取);
进度+=字节读取;
}
}
}
}
我以前从未处理过音频文件,也不知道比特率之类的东西。
如果您有什么想法或建议,请告诉我,因为我被困在这里。

您需要
处理
waveilewriter
才能正确格式化WAV头

更新您的
splitWav
方法:

using(WaveFileWriter writer = new WaveFileWriter(trackPath, reader.WaveFormat)) 
{
   // your code here
}

另外,您需要使用
Write
方法而不是
WriteAsync
,因为
Write
将跟踪写入数据块的字节数

非常感谢您的回答!令人惊讶的是,它现在适用于每一首曲目,但第一首曲目有startPos 0。你知道我怎么解决这个问题吗?