Having floundered several times and linking to this page, I came across this , which helped me with my problem. At first I managed to download the wav file, but subsequently it could be played only once because it could not rewind it due to an error " mark / reset not supported ". It was crazy.
The linked code reads an AudioInputStream from a file, then puts an AudioInputStream in a BufferedInputStream, and then returns it back to an AudioInputStream as follows:
audioInputStream = AudioSystem.getAudioInputStream(new File(filename)); BufferedInputStream bufferedInputStream = new BufferedInputStream(audioInputStream); audioInputStream = new AudioInputStream(bufferedInputStream, audioInputStream.getFormat(), audioInputStream.getFrameLength());
And then, finally, it converts the read data into PCM encoding:
audioInputStream = convertToPCM(audioInputStream);
With convertToPCM is defined as:
private static AudioInputStream convertToPCM(AudioInputStream audioInputStream) { AudioFormat m_format = audioInputStream.getFormat(); if ((m_format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) && (m_format.getEncoding() != AudioFormat.Encoding.PCM_UNSIGNED)) { AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, m_format.getSampleRate(), 16, m_format.getChannels(), m_format.getChannels() * 2, m_format.getSampleRate(), m_format.isBigEndian()); audioInputStream = AudioSystem.getAudioInputStream(targetFormat, audioInputStream); } return audioInputStream; }
I believe they do this because BufferedInputStream handles mark / reset better than audioInputStream. Hope this helps someone out there.
AndyG Nov 28 '11 at 16:43 2011-11-28 16:43
source share