当前位置: 首页 > 工具软件 > NAudio > 使用案例 >

使用NAudio连接WAV音频文件

长孙章横
2023-12-01

引用库

Install-Package NAudio -Version 1.10.0

直接上代码

百度找解决方案找了3天,花33积分下载号称全网唯一能用的代码也不行。谷歌30分钟解决问题。发现有蛮多朋友需要,所以这里分享一下。

class Program
    {
        static void Main(string[] args)
        {
            var list = new DirectoryInfo("C:\\Audio").GetFiles().Select(x=>x.FullName).ToList();
            IList<byte[]> sourceData = new List<byte[]>();
            for (int i = 0; i < list.Count; i++)
            {
                sourceData.Add(GetFileData(list[i]));
            }

            var bytes = Concatenate(sourceData);
            MemoryStream ms = new MemoryStream(bytes);
            SoundPlayer player = new SoundPlayer();
            player.Stream = ms;        
            player.Play();           
                     
            ms.Close();
            ms.Dispose();

        
            Console.ReadKey();
        }

        /// <summary>
        /// 将文件转换成byte[] 数组
        /// </summary>
        /// <param name="fileUrl">文件路径文件名称</param>
        /// <returns>byte[]</returns>
        protected static byte[] GetFileData(string fileUrl)
        {
            FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
            try
            {
                byte[] buffur = new byte[fs.Length];
                fs.Read(buffur, 0, (int)fs.Length);

                return buffur;
            }
            catch (Exception ex)
            {
                //MessageBoxHelper.ShowPrompt(ex.Message);
                return null;
            }
            finally
            {
                if (fs != null)
                {

                    //关闭资源
                    fs.Close();
                }
            }
        }

        public static byte[] Concatenate(IEnumerable<byte[]> sourceData)
        {
            var buffer = new byte[1024 * 4];
            WaveFileWriter waveFileWriter = null;

            using (var output = new MemoryStream())
            {
                try
                {
                    foreach (var binaryData in sourceData)
                    {
                        using (var audioStream = new MemoryStream(binaryData))
                        {
                            using (WaveFileReader reader = new WaveFileReader(audioStream))
                            {
                                if (waveFileWriter == null)
                                    waveFileWriter = new WaveFileWriter(output, reader.WaveFormat);
                                else
                                    AssertWaveFormat(reader, waveFileWriter);

                                WaveStreamWrite(reader, waveFileWriter, buffer);
                            }
                        }
                    }

                    waveFileWriter.Flush();

                    return output.ToArray();
                }
                finally
                {
                    waveFileWriter?.Dispose();
                }
            }
        }

        private static void AssertWaveFormat(WaveFileReader reader, WaveFileWriter writer)
        {
            if (!reader.WaveFormat.Equals(writer.WaveFormat))
            {
                throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
            }
        }

        private static void WaveStreamWrite(WaveFileReader reader, WaveFileWriter writer, byte[] buffer)
        {
            int read;
            while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
            {
                writer.Write(buffer, 0, read);
            }
        }
    }

参考资料

How to join 2 or more .WAV files together programmatically?

 类似资料: