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

时间:2017-10-09 09:37:41

标签: java

我有两个音频文件和 我想使用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"));

我得到以下异常:

  

javax.sound.sampled.UnsupportedAudioFileException:无法从javax.sound.sampled.AudioSystem.getAudioInputStream(未知来源)的输入文件中获取音频输入流

2 个答案:

答案 0 :(得分:1)

得到了解决方案并且它为我工作。

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

答案 1 :(得分:0)

我认为.7mb被视为String wavFile1 = "D://SampleAudio_0.4mb.mp3"; String wavFile2 = "D://wSampleAudio_0.7mb.mp3"; 扩展名。确保不会导致问题。尝试以这种方式重命名文件:

自:

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

要:

{{1}}

<强>更新

我没有看到你已经回答了这个问题,但我认为值得继续关注未来的扩展。