I worked on this answer for my problem. I want that 2 wave files that get of database with byte array type concatenate together and play then dispose it!
this is my code:
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;
}
but I get this error:
Not a WAVE file - no RIFF header
after execute this line:
WaveFileReader wavFileReader = new WaveFileReader(outputSound);
Thanks in advance.
WAV files are not simply an array of bytes, each WAV file has a 44-byte header (the RIFF header) to tell any software how to play it back. Among other things, this header contains information about the length of the file, so in concatenating two WAV files the way you're doing it, you'll end up with two big problems. Firstly, the first WAV file will still have its old header at the start, which will tell your software that the file shorter than it actually is, and secondly, the header of the second WAV file will be stuck in middle of your new file, which will probably sound very strange if you play it back!
So when you're concatenating your files, you'll need to do the following:
WaveFileReader wavFileReader = new
WaveFileReader(outputSound);