简体   繁体   English

无间隙的连接Mp3文件

[英]Concatenate Mp3 Files Without Gap

I've successfully combined 2 mp3 files in sequence. 我已经成功地按顺序组合了2个mp3文件。 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. 我应该提到,我个人从Cubase导出了所有这些音频文件,它们的长度,比特率等完全相同。

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);
            }
        });
    }
}

Gapless play is not possible by design with MP3. MP3的设计无法实现无间隙播放。 The reason for this lies in how the MP3 file format/decoding chain works. 其原因在于MP3文件格式/解码链的工作方式。

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. MP3解码由一个流水线组成,该流水线在将第一个帧馈入解码流水线与实际输出第一个非零样本之间有一点延迟。

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. 虽然您可以轻松地将两个具有相同参数的MP3合并在一起,但通常不会像两个WAV文件那样完美无缺地播放。 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 (eg 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 另请参阅Wikipedia了解更多基础知识http://en.wikipedia.org/wiki/Gapless_playback#Playback_latency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM