简体   繁体   English

如何在java中合并两个mp3文件?

[英]How to merge two mp3 files in java?

i have tow audio files and i want to join that two audio files using java codding or any java Audio-Sound API.我有两个音频文件,我想使用 java codding 或任何 java Audio-Sound API 加入这两个音频文件。

 String wavFile1 = "D://SampleAudio_0.4mb.mp3";
     String wavFile2 = "D://wSampleAudio_0.7mb.mp3";
     AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
     AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));

     AudioInputStream appendedFiles = 
                     new AudioInputStream(
                         new SequenceInputStream(clip1, clip2),     
                         clip1.getFormat(), 
                         clip1.getFrameLength() + clip2.getFrameLength());

     AudioSystem.write(appendedFiles, 
                     AudioFileFormat.Type.WAVE, 
                     new File("D://merge1.mp3"));

I get the following exception:我收到以下异常:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) javax.sound.sampled.UnsupportedAudioFileException: 无法从 javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) 的输入文件获取音频输入流

Got the Solution and It's Working for me.得到了解决方案,它对我有用。

String wavFile1 = "C:\\1.mp3";
String wavFile2 = "C:\\2.mp3";
FileInputStream fistream1 = new FileInputStream(wavFile1);  // first source file
FileInputStream fistream2 = new FileInputStream(wavFile2);//second source file
SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
FileOutputStream fostream = new FileOutputStream("D://merge1.mp3");//destinationfile

int temp;

while( ( temp = sistream.read() ) != -1)
{
    // System.out.print( (char) temp ); // to print at DOS prompt
    fostream.write(temp);   // to write to file
}
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();

I think .7mb.mp3 is recognised as a .7mb extension.我认为.7mb.mp3被认为是.7mb扩展名。 Make sure that's not causing problems.确保这不会导致问题。 Try to rename your files this way:尝试以这种方式重命名您的文件:

From:从:

String wavFile1 = "D://SampleAudio_0.4mb.mp3";
String wavFile2 = "D://wSampleAudio_0.7mb.mp3";

To:到:

String wavFile1 = "D://SampleAudio_01.mp3";
String wavFile2 = "D://wSampleAudio_02.mp3";

Update更新

I didn't see that you have answered the question already, but I think it's worth keeping on eye on the extensions in the future.我没有看到你已经回答了这个问题,但我认为值得关注未来的扩展。

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

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