Reputation: 2285
public AudioInputStream append(AudioInputStream main, AudioInputStream s) throws UnsupportedAudioFileException, IOException {
AudioSystem.write(new AudioInputStream(
new SequenceInputStream(main, s),
main.getFormat(),
main.getFrameLength() + s.getFrameLength()), AudioFileFormat.Type.WAVE, new File("/home/nikkka/Desktop/alphabet/result_for_watch.wav"));
return AudioSystem.getAudioInputStream(new File("/home/nikkka/Desktop/alphabet/result_for_watch.wav"));
}
It's supposed to append audio files, but it always returns the last file, not both of them!... What's the problem? :S
Upvotes: 2
Views: 229
Reputation: 7737
The only way I can see this only returning the second (s) AudioInputStream is if there was a problem reading in the first one. I would suggest breaking your method down some. Here I have expanded the method some in order to add some logging statements and make it more verbose. I did not change anything functional.
public AudioInputStream append(AudioInputStream main, AudioInputStream s)
throws UnsupportedAudioFileException, IOException {
SequenceInputStream sis = new SequenceInputStream(main, s);
long length = main.getFrameLength() + s.getFrameLength();
logger.debug(main.getFrameLength() + "+" + s.getFrameLength() +
"=" + length);
AudioFormat fmt = main.getFormat();
File file = new File("c:/MyNew.wav"); //changed for testing
AudioInputStream ais = new AudioInputStream(sis, fmt, length);
int size = AudioSystem.write( ais, AudioFileFormat.Type.WAVE, file);
logger.debug("Wrote :" + size);
return AudioSystem.getAudioInputStream(file);
}
Also, ensure you are catching and logging your errors in case something is thrown. In my test case everything worked ok. Check the log statement added which shows the size of the file before and after, also they ensure what is written is >= to that value.
Making the method more verbose will help locate the problem. With it more verbose like this will allow you to step through with a debugger easier as well. Once you find the source of the problem you can revert back to the more compact way if you wish.
Upvotes: 1