I have a MemoryStream
that reads in audio from online and then saves it to FileStream
. The audio playback plays fine with the one problem that the first 30-40 milliseconds of the clip is dead sound and silent at the beginning of each split. This is problematic because I need to divide audio clips into multiple files to later seamlessly be played. Is there a certain byte or hex in mp3 data that indicates where a frame of an mp3 starts/finishes to try and split it at the right position? What might this look like in code?
In this simple FileStream
save here, you can see that in my experimenting even if I try to clip out that dead spot at the beginning, it still creates deadspace - leading me to think I am splitting the sound file at an incorrect position and then the player cannot read that frame - but how can I overcome this?
byte[] byteArray = ms.ToArray();
using (FileStream fs = new FileStream(destPath + filename + ".mp3", FileMode.Create))
{
fs.Write(byteArray, 0, byteArray.Length);
}
Clipping back to try and get rid of that dead space at beginning just to test:
byte[] oldArray = ms.ToArray();
byte[] newArray = new byte[oldArray.Length - 1000];
Array.Copy(oldArray, 1000, newArray, 0, newArray.Length);
using (FileStream fs = new FileStream(destPath + filename + ".mp3", FileMode.Create))
{
fs.Write(newArray, 0, newArray.Length);
}
Inspecting the byte array of the mp3 being saved, there is data being saved even in that dead space when I convert it to text but for some reason it just doesn't play. As I said, I'm guessing it is something to do with the file being split incorrectly half way through a sound frame or something? Sorry if I have used incorrect terminology.