Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

java - Javasound: AudioSystem.getAudioInputStream fails on server, works in client code

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...