简体   繁体   中英

Setting a Timeout on an AudioInputStream

I am playing audio received through an AudioInputStream . The audio data is stored on a remote server.

Retrieving the AudioInputStream via AudioSystem.getAudioInputStream(new URL(path)) works perfectly, however I require a timeout on this InputStream .

I thought I might be able to create a Socket connection to the URL and then utilise the Socket timeout, however AudioSystem.getAudioInputStream(InputStream) requires the InputStream to be mark supported. This is when I wrapped the Socket InputStream in a BufferedInputStream and received the following error message:

java.io.EOFException
    at java.io.DataInputStream.readInt(Unknown Source)
    at com.sun.media.sound.WaveFileReader.getFMT(Unknown Source)
    at com.sun.media.sound.WaveFileReader.getAudioInputStream(Unknown Source)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)

Working code:

audioInputStream = AudioSystem.getAudioInputStream(new URL(path));

Code that causes EOFException :

audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(socket.getInputStream()));

Why is the EOFException occurring, and is it possible to add a timeout to an AudioInputStream ?

By replacing the Socket with a URLConnection , I was able to resolve the issue. I'm assuming this is because the Socket connection (at least in the manner I set it up) opened an input stream to the actual server (server.com) rather than a file on the server as I intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM