Reputation: 311
I've successfully combined 2 mp3 files in sequence. Unfortunately, these files have a very small but noticeable gap between them which makes it very obvious that they are 2 separate files.
I should mention that I personally exported all these audio files from Cubase with the exact same length, bit rate, etc.
I've looked online and I can't find much information about this.
My question is:
Is there a way to cut off a tiny amount at the end of a file or crossfade the files?
I feel like as it is, even crossfading would potentially still have a gap.
Thanks for any help!
Here is my code:
public void myMethod1() throws IOException
{
File newFile=new File(Environment.getExternalStorageDirectory()
+File.separator
+"Music"
+File.separator
+"MyApp"+File.separator+"MergedFile.mp3");
File newFile1=new File(Environment.getExternalStorageDirectory()
+File.separator
+"Music"
+File.separator
+"MyApp"+File.separator+"file104.mp3");
File newFile2=new File(Environment.getExternalStorageDirectory()
+File.separator
+"Music"
+File.separator
+"MyApp"+File.separator+"file105.mp3");
Log.d("newFile", newFile.toString());
Log.d("newFile1", newFile1.toString());
Log.d("newFile2", newFile2.toString());
FileInputStream fistream1 = new FileInputStream(newFile1 ); // first source file
FileInputStream fistream2= new FileInputStream(newFile2 );//second source file
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(fistream1);
v.add(fistream2);
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
if(!newFile.exists()){
newFile.createNewFile();
FileOutputStream fostream=new FileOutputStream(newFile, true);
int temp;
while( ( temp = sistream.read() ) != -1)
{
//System.out.print( (char) temp ); // to print at DOS prompt
fostream.write((byte)temp); // to write to file
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();
MediaScannerConnection.scanFile(this,
new String[] { newFile.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
}
Upvotes: 1
Views: 1068
Reputation: 20059
Gapless play is not possible by design with MP3. The reason for this lies in how the MP3 file format/decoding chain works.
MP3 decoding consists of a pipeline that has a slight delay between feeding the first frame into the decoding pipeline and actually outputting the first non-zero samples.
While you can concatinate two MP3's with identical parameters easily, this will generally not result in flawless gapless playback, as it would with lets say two WAV files. To ensure that the entire sound is actually output the encoder will add a few (don't know the exact amount, probably varies with input length and frequency) padding frames that essentially contain silence at the very end of the stream (that are never output, if played normally, they just push the real data to the output stage).
If the idea is to create chunks that can be combined on the fly (e.g. for game music), you could use a defined amount of overlap and throw away a set amount of samples at the start/end of each chunk to achieve gapless playback. This won't work generically, although some audio players may support this using custom playlist tags for hinting the process.
Also see Wikipedia for more fundamentals http://en.wikipedia.org/wiki/Gapless_playback#Playback_latency
Upvotes: 1