/// <summary>/// 读取wav文件然后剪切/// </summary>/// <param name="inPath">目标文件</param>/// <param name="outPath">输出文件</param>/// <param name="cutFromStart">开始时间</param>/// <param name="cutFromEnd">结束时间</param>public void TrimWavFile(string inPath, string outPath, TimeSpan cutFromStart, TimeSpan cutFromEnd){using (WaveFileReader reader = new WaveFileReader(inPath)){using (WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat)){int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;int startPos = (int)cutFromStart.TotalMilliseconds * bytesPerMillisecond;startPos = startPos - startPos % reader.WaveFormat.BlockAlign;int endBytes = (int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;//int endPos = (int)reader.Length - endBytes;TrimWavFile(reader, writer, startPos, endBytes);}}}/// <summary>/// 重新合并wav文件/// </summary>/// <param name="reader">读取流</param>/// <param name="writer">写入流</param>/// <param name="startPos">开始流</param>/// <param name="endPos">结束流</param>private static void TrimWavFile(WaveFileReader reader, WaveFileWriter writer, int startPos, int endPos){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){writer.WriteData(buffer, 0, bytesRead);}}}}
导入NAudio.dll
下载https://download.csdn.net/download/qq_33670936/10548691