I found a lot of articles about how to join mp3 or wav files,but i didn't find nothing about how to join audio files of different types.I want to play wav file then mp3 file,one after another. I tried to convert wav file to mp3 by using.
private static MemoryStream ConvertWavToMp3(string path)
{
if (File.Exists(path))
{
MemoryStream stream = new MemoryStream();
using (WaveFileReader rdr = new WaveFileReader(path))
using (LameMP3FileWriter wtr = new LameMP3FileWriter(stream, rdr.WaveFormat, 320))
{
rdr.CopyTo(wtr);
return stream;
}
}
else
{
return null;
}
}
}
I get bytes from mp3 file by following code.
private static MemoryStream GetBytesFromMp3(string path)
{
string storageName = path.Substring(path.IndexOf("\\\\") + 2, path.IndexOf("\\", 3) - path.IndexOf("\\\\") - 2);
string networkShare = path.Substring(0, path.LastIndexOf("\\"));
NetworkCredential credentials = new NetworkCredential(ConfigurationManager.AppSettings[storageName + "_User"], ConfigurationManager.AppSettings[storageName + "_Pass"]);
using (new NetworkConnection(networkShare, credentials))
{
if (File.Exists(path))
{
using (Mp3FileReader rdr = new Mp3FileReader(path,FileMode.OpenOrCreate))
{
byte[] result = new byte[rdr.Length];
rdr.Read(result, 0, result.Length);
int a = result.Max();
MemoryStream stream = new MemoryStream(result);
return stream;
}
}
else
{
return null;
}
}
}
and and then merge them by
public static byte[] JoinFiles(List<FileWeb> files)
{
MemoryStream output = new MemoryStream();
int offset = 0;
foreach (FileWeb file in files)
{
MemoryStream mp3Data;
string fullPath = file.FilePath + "\\" + file.FileName;
if (file.FileName.EndsWith("wav"))
{
mp3Data = ConvertWavToMp3(fullPath);
}
else
{
mp3Data = GetBytesFromMp3(fullPath);
}
if (mp3Data != null)
{
byte[] buffer = mp3Data.ToArray();
output.Write(buffer, offset, buffer.Length - offset);
offset = 4;
}
}
return output.ToArray();
}
But it didn't work.It plays only first part.If i use this code.
public static void Combine(string[] inputFiles, Stream output)
{
foreach (string file in inputFiles)
{
Mp3FileReader reader = new Mp3FileReader(stream);
if ((output.Position == 0) && (reader.Id3v2Tag != null))
{
output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
}
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
output.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}
at Mp3FileReader reader = new Mp3FileReader(stream)
i get error no mp3 header found.
Do not convert anything to MP3. Just create appropriate reader for given file (WaveFileReader
for wav
and Mp3FileReader
for mp3
). Both classes derives from WaveStream
which provides Read
method. Use that method to retrieve uncompressed data. Now you can easily merge two streams. Of course WaveFormat
must match.
But you don't need to reinvent wheel. There is built in concatenation mechanism.
var wavReader = new WaveFileReader ("file.wav");
var mp3Reader = new Mp3FileReader ("file.mp3");
// convert to different interface
var wavProvider = wavReader.ToSampleProvider ();
var mp3Provider = mp3Reader.ToSampleProvider ();
// Must all share the same sample rate and channel count
var merged = new ConcatenatingSampleProvider (new[] { wavProvider, mp3Provider });
var output = new WasapiOut ();
output.Init (merged);
output.Play ();
Console.ReadKey ();
Good luck.