简体   繁体   中英

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.

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.

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 can (usually) repair the errors caused by crudely joining two files, and is available for many platforms.

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.

//Convrert Mp3 To 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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