我需要能够将 2 个或多个 .wav 文件合并为一个 .wav 文件。我必须使用 C# 以编程方式执行此操作(不能选择第 3 方产品)。我知道 System.Media.SoundPlayer 类,但我不想播放 .wav,而只是创建它。
这是使用构建的基本 WAV 连接函数NAudio http://naudio.codeplex.com。这将确保仅连接数据块(与中的代码示例不同)这篇 CodeProject 文章 http://www.codeproject.com/KB/audio-video/Concatenation_Wave_Files.aspx链接在另一个答案中)。它还可以防止您串联不同格式的 WAV 文件。
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();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)