Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在流中连接2个wave文件?_C#_Naudio - Fatal编程技术网

C# 如何在流中连接2个wave文件?

C# 如何在流中连接2个wave文件?,c#,naudio,C#,Naudio,为了我的问题我做了这个。 我希望2波文件,得到的数据库字节数组类型连接在一起,发挥然后处置它 这是我的代码: public static void Play() { List<byte[]> audio = dal.SelectSound("خدمات", "احیاء"); byte[] sound = new byte[audio[0].Length + audio[1].Length]; Stream outputSound = Conca

为了我的问题我做了这个。 我希望2波文件,得到的数据库字节数组类型连接在一起,发挥然后处置它

这是我的代码:

 public static void Play()
 {
     List<byte[]> audio = dal.SelectSound("خدمات", "احیاء");

     byte[] sound = new byte[audio[0].Length + audio[1].Length];

     Stream outputSound = Concatenate(sound, audio);

     try
     {
           WaveFileReader wavFileReader = new WaveFileReader(outputSound);
           var waveOut = new WaveOut(); // or WaveOutEvent()
           waveOut.Init(wavFileReader);
           waveOut.Play();
      }
      catch (Exception ex)
      {
           Logs.ErrorLogEntry(ex);
      }
}

 public static Stream Concatenate(byte[] outputFile, List<byte[]> sourceFiles)
 {
        byte[] buffer = new byte[1024];

        Stream streamWriter = new MemoryStream(outputFile);

        try
        {
            foreach (byte[] sourceFile in sourceFiles)
            {
                Stream streamReader = new MemoryStream(sourceFile);

                using (WaveFileReader reader = new WaveFileReader(streamReader))
                {
                    int read;
                    while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        streamWriter.Write(buffer, 0, read);
                    }
                }
            }
        }

        return streamWriter;
    }

提前感谢。

WAV文件不仅仅是一个字节数组,每个WAV文件都有一个44字节的头(RIFF头),告诉任何软件如何播放它。除此之外,该头文件还包含有关文件长度的信息,因此,以这种方式连接两个WAV文件时,您将遇到两个大问题。首先,第一个WAV文件在开始时仍然有其旧的头,这将告诉您的软件该文件比实际文件短,其次,第二个WAV文件的头将卡在新文件的中间,如果您播放它,这可能听起来很奇怪

因此,在连接文件时,需要执行以下操作:

  • 删除每个文件的前44个字节
  • 连接两个字节 阵列
  • 根据标题创建新标题
  • 把这个标题放在最前面 连接字节数组的前面
  • 调用WaveFileReader wavFileReader=new 波形阅读器(输出声音)

  • 使用WaveFileWriter创建一个新的wave文件。WaveFileWriter仅用于在磁盘上写入!谢谢,非常有用的答案,但是我用另一个非常简单的解决方案解决了我的问题;-)@amirstack,你能提供solution@BabuJames我不确定我的解决方案是否对您有用,但我不加入wav文件!我先播放第一个文件,然后再播放第二个文件,并且在某些情况下使用CSCore库来避免语音文件重叠。请告诉我此解决方案是否对您有用,是否需要更多说明。我从第二个WAV/RIFF中删除了标题,并增加了第一个标题的文件大小-播放器仅播放该组合文件中的第一个音频,文件大小会增加,但即使在
    ffprobe
    中,我也只看到第一个音频的持续时间:(
    WaveFileReader wavFileReader = new WaveFileReader(outputSound);