1

i have tow audio files and i want to join that two audio files using java codding or any 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)

Dmitry
  • 6,716
  • 14
  • 37
  • 39
Sajan Parmar
  • 85
  • 1
  • 10

2 Answers2

4

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();
Sajan Parmar
  • 85
  • 1
  • 10
  • Note for future readers: This solution will not work in most cases as you will just have two back-to-back files stored in bytes both with file headers and therefore not a true single merged 3p3 file. Many applications that read mp3 files will likely give an error or only read the first file. The correct solution is to merge the audio content and update the header to reflect it, but rather than doing it yourself the best method is to use one of the many existing audio libraries which can be found with a quick search. – sorifiend Jul 31 '22 at 06:15
  • @sorifiend can you recommend me a java library to achieve this? – xBurnsed May 22 '23 at 15:12
0

I think .7mb.mp3 is recognised as a .7mb extension. 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.

Endre Börcsök
  • 477
  • 1
  • 8
  • 19