I'm working on an audio project in java that requires me to determine audio file types based on their data (as opposed to file extension) and I've hit a wall with MP3s. As far as I understand, MP3 files are broken into frames where each frame has a 4 byte header containing 11's for a frame sync and an assortment of other data. Right now my code can accurately identify a WAVE file but when I start reading my test MP3 file's bytes I can't find a 11111111 byte (the first 8 of 11 frame sync bits) anywhere.
try {
FileInputStream fis = new FileInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[11];
byte[] byteData;
int bytes = 0;
while ((bytes = fis.read(buff)) != -1) {
baos.write(buff, 0, buff.length);
}
byteData = baos.toByteArray();
fis.close();
baos.close();
if ((int)byteData[0] == 255) {
type = "MP3";
} else if (("" + (char)byteData[8] + (char)byteData[9] +
(char)byteData[10] + (char)byteData[11]) == "WAVE") {
type = "WAVE";
}
}