如何加入2个或更多.WAV文件一起编程? [英] How to join 2 or more .WAV files together programatically?

查看:122
本文介绍了如何加入2个或更多.WAV文件一起编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加入2个或更多的.wav能力的文件一起在一个.wav文件。我必须编程做到这一点,使用C#(第三方产品是不是一个选项)。我知道System.Media.SoundPlayer类的,但我不是找打的.WAV,但只创造它。

I need the ability to join 2 or more .wav files together in to one .wav file. I must do this programatically, using C# (3rd-party products are not an option). I know of the System.Media.SoundPlayer class, but I am not looking to play the the .wav, but only to create it.

先谢谢了。

推荐答案

下面是一个使用构建了一个基本WAV连接函数 n音讯。这将确保只有数据块被连接(在不同的此$ C $的CProject文章在另一个答案的链接)。它也将保护您免受串联不共享同一格式的WAV文件。

Here's a basic WAV concatenation function built using NAudio. This will ensure that only the data chunks are concatenated (unlike the code example in this CodeProject article linked in another answer). It will also protect you against concatenating WAV files that do not share the same format.

public static void Concatenate(string outputFile, IEnumerable<string> sourceFiles)
{
    byte[] buffer = new byte[1024];
    WaveFileWriter waveFileWriter = null;

    try
    {
        foreach (string sourceFile in sourceFiles)
        {
            using (WaveFileReader reader = new WaveFileReader(sourceFile))
            {
                if (waveFileWriter == null)
                {
                    // first time in create new Writer
                    waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                }
                else
                {
                    if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                    {
                        throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                    }
                }

                int read;
                while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    waveFileWriter.WriteData(buffer, 0, read);
                }
            }
        }
    }
    finally
    {
        if (waveFileWriter != null)
        {
            waveFileWriter.Dispose();
        }
    }

}

这篇关于如何加入2个或更多.WAV文件一起编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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