I'm trying to create a karaoke system in Java, I have a music MP3 file and a file which I recorded by singing, I want them to overlap and save it as another file., So how do I do it?
I tried writing a buffer from one file and the second from the other file and kept it on repeat till the end of the file but It didn't work out I also tried concatenating them but the program just added the second file at the end of the first file and saved it as a third file, I didn't work either.
File f1 = new File("D:/Background OST 2.MP3");
InputStream is = new FileInputStream(f);
InputStream is1 = new FileInputStream(f);
OutputStream outstream = new FileOutputStream(new File("D:/blabla.MP3"));
byte[] buffer = new byte[4096];
byte[] buffer1 = new byte[4096];
int len = 0;
int len1 = 0;
int index = 0;
boolean complete = false , complete1 = false;
while(true){
if(!complete){
if((len = is.read((buffer))) > 0){
outstream.write(buffer, 0, len);
}else{
complete = true;
}
}
if(!complete1){
if((len1 = is1.read((buffer1))) > 0){
outstream.write(buffer1, 0, len1);
}else{
complete1 = true;
}
}
byte b = -1;
if(buffer.length > index){
if(buffer[index] > 0){
b = buffer[index];
}
}
if(buffer1.length > index){
if(buffer1[index] > 0){
b += buffer1[index];
}
}
//outstream.write(b, 0, len1);
outstream.write(b);
if(complete == true && complete1 == true){
break;
}
index++;
}
outstream.flush();
// closing resources
is.close();
is1.close();
outstream.close();
I expected the code to overlap a music file and another file that I recorded via mic but Instead of that the new file concatenate the sounds and there were too many weird noises in it.