Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My application involves uploading a wave file via a web UI, converting format to 8k, 8 bit mulaw and storing it on the server. My code is failing on the server when trying to do:

final AudioInputStream ais = AudioSystem.getAudioInputStream( in );

The error is:

Caused by: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1102) [classes.jar:1.6.0_65]

So I scaled back to a simple test case where I try to load the audio file as a local file:

final File audioFile = new File("/path/to/audio.wav");
final InputStream in = new FileInputStream( file );
final AudioInputStream ais = AudioSystem.getAudioInputStream( in );

This works. I then went back to my server side and added some debugging like so:

    final byte[] audio = IOUtils.toByteArray( in );
    final File audioLog = File.createTempFile( "audiolog", ".wav" );
    IOUtils.write( audio, new FileOutputStream( audioLog ) );
    s_logger.info( "File logged to: " + audioLog.getAbsolutePath() );
    final InputStream byteIn = new ByteArrayInputStream( audio );
    final AudioInputStream ais = AudioSystem.getAudioInputStream( byteIn ); 

This fails in the exact same way.

I then compare the original file that was uploaded against the audio file logged on the server and they are identical, right down to the checksum:

$ cksum uploaded.wav 
4019972581 84076 uploaded.wav 
$ cksum audiolog6415586848170376004.wav 
4019972581 84076 audiolog6415586848170376004.wav

Any ideas what may be going on? My server side code run on JBoss 7.1.

Thanks.

-Raj

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
237 views
Welcome To Ask or Share your Answers For Others

1 Answer

The following might be worth trying.

The InputStream intermediate step does various tests that can throw an IOException if they fail. From the api:

The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException.

I recommend avoiding the InputStream step altogether and set yourself up to load via AudioSystem.getAudioInputStream(File) or AudioSystem.getAudioInputStream(URL). These two do not have this requirement.

I will admit, though, this does not jibe with your writing that the error thrown was UnsupportedAudioFileException, and that you were able to load using this method as a "local file". The way in which these tests are implemented could be different in the different circumstances.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...