简体   繁体   English

如何在 Java 中合并两个 MP3 文件并在 Windows Media Player 中播放结果?

[英]How To Merge Two MP3 Files in Java and Play the result in Windows Media Player?

In the Below Code Two mp3 files are merged but merged audio file is only played in VLC media player, and not in windows Media Player.在下面的代码中,合并了两个 mp3 文件,但合并的音频文件只能在 VLC 媒体播放器中播放,而不能在 Windows 媒体播放器中播放。

import java.io.*;
public class TwoFiles
{
    public static void main(String args[]) throws IOException
    {
        FileInputStream fistream1 = new FileInputStream("C:\\Temp\\1.mp3"); 
        FileInputStream fistream2 = new FileInputStream("C:\\Temp\\2.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        FileOutputStream fostream = new FileOutputStream("C:\\Temp\\final.mp3");
        int temp;
        while( ( temp = sistream.read() ) != -1)
        {
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}

It's worth checking whether the output file is the expected length, so that the two files have actually been concatenated.值得检查输出文件是否是预期的长度,以便两个文件实际上已经连接起来。 However, many media players won't accept an MP3 file like this -- there are all sorts of frame headers that end up in the wrong place, not to mention ID3 tags, etc. It's a tribute to the robustness of players like VLC and mplayer that they can actually handle MP3 files that are so poorly-structured.然而,许多媒体播放器不会接受这样的 MP3 文件——有各种各样的帧头最终出现在错误的位置,更不用说 ID3 标签等了。这是对像 VLC 和mplayer,他们实际上可以处理结构如此糟糕的 MP3 文件。

The "proper" way to handle this situation is to stream out the actual audio data, and then add back the tags, etc., as required.处理这种情况的“正确”方法是输出实际的音频数据,然后根据需要重新添加标签等。 This is a bear, frankly, and a quick-and-dirty approach that I have found to work quite well is to run mp3val on the concatenated files.坦率地说,这是一种负担,我发现一种快速而肮脏的方法是对连接的文件运行mp3val mp3val can (usually) repair the errors caused by crudely joining two files, and is available for many platforms. mp3val可以(通常)修复粗略加入两个文件导致的错误,并且适用于许多平台。

Use Java Zoom Library To Convert MP3 File to WAVE File then Combine Both WAVE File and then By Using jave-1.0.2.jar Convert the combined WAVE File to MP3 File.使用 Java Zoom 库将 MP3 文件转换为 WAVE 文件,然后合并两个 WAVE 文件,然后使用 jave-1.0.2.jar 将合并的 WAVE 文件转换为 MP3 文件。

//Convrert Mp3 To Wavw //将 Mp3 转换为 Wavw

Converter myConverter = new Converter();
myConverter.convert("D://1476501067.2281665_0.mp3","D://1476501067.2281665_0.mp3"+".wav");

//Combine Both Wave Files //合并两个波形文件

 File sample1 = new File("F://StaticVoice.wav");
                                        File sample2 = new File("F://ChangeVoice");
                                           File fileOut = new File("F://MyTest.wav");//WAVEFiles

                                         AudioInputStream audio1 = AudioSystem.getAudioInputStream(sample1);
                                           AudioInputStream audio2 = AudioSystem.getAudioInputStream(sample2);

                                           AudioInputStream audioBuild = new AudioInputStream(new SequenceInputStream(audio1, audio2), audio1.getFormat(), audio1.getFrameLength() + audio2.getFrameLength());

                                           for(int i = 0; i < 5; i++){
                                               audioBuild = new AudioInputStream(new SequenceInputStream(audioBuild, audio2), audioBuild.getFormat(), audioBuild.getFrameLength() + audio2.getFrameLength());

                                           }

                                           AudioSystem.write(audioBuild, AudioFileFormat.Type.WAVE, fileOut);

//Convert WAVE File TO MP3 //将 WAVE 文件转换为 MP3

File source = new File("F://Merge.wav");
                             File target = new File("F://Merge.mp3"); 
                                 AudioAttributes audio = new AudioAttributes();
                                 audio.setCodec("libmp3lame");
                                 audio.setBitRate(new Integer(128000));
                                 audio.setChannels(new Integer(1));
                                 audio.setSamplingRate(new Integer(44100));
                                 EncodingAttributes attrs = new EncodingAttributes();
                                 attrs.setFormat("mp3");
                                 attrs.setAudioAttributes(audio);
                                 Encoder encoder = new Encoder();
                                 encoder.encode(source, target, attrs);

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

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