AudioSystem.getFileFormat() hangs

T

Turner

Hello, Java gurus,

I'm currently writing an applet that uses the Java Sound API. I'm
encountering a strange problem in obtaining a SourceDataLine. I try
setting up two lines for two different WAV files (unencoded PCM,
nothing fancy) over the internet (although they're currently on
localhost). The first gets a line just fine. When I try to create a
second one, though, it hangs indefinitely. So I added debugging
printlns, and it appears to be getting stuck at the call to
AudioSystem.getAudioFileFormat(URL u)--the statement "Getting audio
format" is never printed. I can't for the life of me figure out what's
going on. As you can see below, I'm catching generic exceptions, so it
can't be an exception that somehow slipped by.

It can't be something wrong with the second file, because I got the
same problem when I tried to get the lines in the opposite order. Does
anyone see anything suspect in the code below/know what could be
causing this problem?



=================CODE=========================

public SourceDataLine line;
public URL url;
public AudioInputStream stream;

public StreamingSound(String url) throws
MalformedURLException,
UnsupportedAudioFileException
{
this.url = new URL(url);
try {
stream = AudioSystem.getAudioInputStream(this.url);
debug("Got an input stream");
}
catch(IOException e) {
e.printStackTrace();
}
debug("About to try getting the line");
try {
debug("Getting file format at URL: " + this.url);
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(this.url);
debug("Getting audio format");
AudioFormat format = fileFormat.getFormat();
debug("Getting DataLine.Info");
DataLine.Info info = new
DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Line " + format + " not
supported");
}
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
debug("Opened the line");
} catch (LineUnavailableException e) {
System.err.println("Line not available.");
e.printStackTrace();
System.exit(0);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch(Exception e) {
System.err.println("Exception encountered: " +
e.getMessage());
e.printStackTrace();
}
}

===========================================
 
K

Knute Johnson

Turner said:
Hello, Java gurus,

I'm currently writing an applet that uses the Java Sound API. I'm
encountering a strange problem in obtaining a SourceDataLine. I try
setting up two lines for two different WAV files (unencoded PCM,
nothing fancy) over the internet (although they're currently on
localhost). The first gets a line just fine. When I try to create a
second one, though, it hangs indefinitely. So I added debugging
printlns, and it appears to be getting stuck at the call to
AudioSystem.getAudioFileFormat(URL u)--the statement "Getting audio
format" is never printed. I can't for the life of me figure out what's
going on. As you can see below, I'm catching generic exceptions, so it
can't be an exception that somehow slipped by.

It can't be something wrong with the second file, because I got the
same problem when I tried to get the lines in the opposite order. Does
anyone see anything suspect in the code below/know what could be
causing this problem?



=================CODE=========================

public SourceDataLine line;
public URL url;
public AudioInputStream stream;

public StreamingSound(String url) throws
MalformedURLException,
UnsupportedAudioFileException
{
this.url = new URL(url);
try {
stream = AudioSystem.getAudioInputStream(this.url);
debug("Got an input stream");
}
catch(IOException e) {
e.printStackTrace();
}
debug("About to try getting the line");
try {
debug("Getting file format at URL: " + this.url);
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(this.url);
debug("Getting audio format");
AudioFormat format = fileFormat.getFormat();
debug("Getting DataLine.Info");
DataLine.Info info = new
DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Line " + format + " not
supported");
}
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
debug("Opened the line");
} catch (LineUnavailableException e) {
System.err.println("Line not available.");
e.printStackTrace();
System.exit(0);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch(Exception e) {
System.err.println("Exception encountered: " +
e.getMessage());
e.printStackTrace();
}
}

===========================================

Since all of your program isn't here to test, try the simple program
below. Substitute your own audio file names. It should play both of
them at the same time.

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class AudioTest implements Runnable {
final SourceDataLine sdl;
final AudioInputStream ais;

public AudioTest(String urlString) throws Exception {
URL url = new URL(urlString);
AudioFileFormat aff = AudioSystem.getAudioFileFormat(url);
AudioFormat af = aff.getFormat();
ais = AudioSystem.getAudioInputStream(url);
sdl = AudioSystem.getSourceDataLine(af);
}

public void run() {
try {
sdl.open();
sdl.start();
int bytesRead;
byte[] buf = new byte[1024];
while ((bytesRead = ais.read(buf)) != -1)
sdl.write(buf,0,bytesRead);
} catch (Exception e) {
e.printStackTrace();
} finally {
sdl.drain();
sdl.stop();
sdl.close();
}
}

public static void main(String[] args) throws Exception {
AudioTest t1 = new AudioTest("file:22-new.aif");
new Thread(t1).start();
AudioTest t2 = new AudioTest("file:1-welcome.wav");
new Thread(t2).start();
}
}
 
A

Andrew Thompson

Hello, Java gurus,

I'm currently writing an applet that uses the Java Sound API. I'm
encountering a strange problem in obtaining a SourceDataLine. I try
setting up two lines for two different WAV files (unencoded PCM,
nothing fancy) over the internet (although they're currently on
localhost). The first gets a line just fine. When I try to create a
second one, though, it hangs indefinitely. ...

Not here. Note that I turned your code into
an SSCCE* that works for files on this machine.

<sscce>
import javax.sound.sampled.*;
import java.net.*;
import java.io.IOException;
import java.io.File;

class StreamingSound
{
public SourceDataLine line;
public URL url;
public AudioInputStream stream;

public StreamingSound(String url) throws
MalformedURLException,
UnsupportedAudioFileException
{
this.url = new URL(url);
try
{
stream = AudioSystem.
getAudioInputStream(this.url);
System.out.println("Got an input stream");
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("About to try getting the line");
try
{
System.out.println(
"Getting file format at URL: " + this.url);
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(this.url);
System.out.println("Getting audio format");
AudioFormat format = fileFormat.getFormat();
System.out.println("Getting DataLine.Info");
DataLine.Info info = new
DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info))
{
System.err.println(
"Line " +
format +
" not supported");
}
try {
line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(format);
System.out.println("Opened the line");
//line.close();
} catch (LineUnavailableException e)
{
System.err.println("Line not available.");
e.printStackTrace();
System.exit(0);
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.err.println("Exception encountered: " +
e.getMessage());
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception
{
StreamingSound ss1 = new StreamingSound(
"file:/C:/Media/media/wav/100_2819.wav");
StreamingSound ss2 = new StreamingSound(
"file:/C:/Media/media/wav/100_2820.wav");
}
}
</sscce>

[OP]
Got an input stream
About to try getting the line
Getting file format at URL: file:/C:/Media/media/wav/100_2819.wav
Getting audio format
Getting DataLine.Info
Opened the line
Got an input stream
About to try getting the line
Getting file format at URL: file:/C:/Media/media/wav/100_2820.wav
Getting audio format
Getting DataLine.Info
Opened the line
Press any key to continue . . .
[/OP]

Java 1.6.0_10-beta, Win XP Pro.

* please consider posting SSCCE's in future.
<http://sscce.org/>
 
T

Turner

Turner said:
Hello, Java gurus,
I'm currently writing an applet that uses the Java Sound API. I'm
encountering a strange problem in obtaining a SourceDataLine. I try
setting up two lines for two different WAV files (unencoded PCM,
nothing fancy) over the internet (although they're currently on
localhost). The first gets a line just fine. When I try to create a
second one, though, it hangs indefinitely. So I added debugging
printlns, and it appears to be getting stuck at the call to
AudioSystem.getAudioFileFormat(URL u)--the statement "Getting audio
format" is never printed. I can't for the life of me figure out what's
going on. As you can see below, I'm catching generic exceptions, so it
can't be an exception that somehow slipped by.
It can't be something wrong with the second file, because I got the
same problem when I tried to get the lines in the opposite order. Does
anyone see anything suspect in the code below/know what could be
causing this problem?

public SourceDataLine line;
public URL url;
public AudioInputStream stream;
public StreamingSound(String url) throws
MalformedURLException,
UnsupportedAudioFileException
{
this.url = new URL(url);
try {
stream = AudioSystem.getAudioInputStream(this.url);
debug("Got an input stream");
}
catch(IOException e) {
e.printStackTrace();
}
debug("About to try getting the line");
try {
debug("Getting file format at URL: " + this.url);
AudioFileFormat fileFormat =
AudioSystem.getAudioFileFormat(this.url);
debug("Getting audio format");
AudioFormat format = fileFormat.getFormat();
debug("Getting DataLine.Info");
DataLine.Info info = new
DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
System.err.println("Line " + format + " not
supported");
}
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
debug("Opened the line");
} catch (LineUnavailableException e) {
System.err.println("Line not available.");
e.printStackTrace();
System.exit(0);
}
}
catch(IOException e) {
e.printStackTrace();
}
catch(Exception e) {
System.err.println("Exception encountered: " +
e.getMessage());
e.printStackTrace();
}
}
===========================================

Since all of your program isn't here to test, try the simple program
below. Substitute your own audio file names. It should play both of
them at the same time.

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class AudioTest implements Runnable {
final SourceDataLine sdl;
final AudioInputStream ais;

public AudioTest(String urlString) throws Exception {
URL url = new URL(urlString);
AudioFileFormat aff = AudioSystem.getAudioFileFormat(url);
AudioFormat af = aff.getFormat();
ais = AudioSystem.getAudioInputStream(url);
sdl = AudioSystem.getSourceDataLine(af);
}

public void run() {
try {
sdl.open();
sdl.start();
int bytesRead;
byte[] buf = new byte[1024];
while ((bytesRead = ais.read(buf)) != -1)
sdl.write(buf,0,bytesRead);
} catch (Exception e) {
e.printStackTrace();
} finally {
sdl.drain();
sdl.stop();
sdl.close();
}
}

public static void main(String[] args) throws Exception {
AudioTest t1 = new AudioTest("file:22-new.aif");
new Thread(t1).start();
AudioTest t2 = new AudioTest("file:1-welcome.wav");
new Thread(t2).start();
}

}

Interesting. This works. Could it really just be that I didn't thread
it? If so, what's blocking? As far as I can tell, there's no limit to
the number of lines allowed to be obtained in a single thread, is
there?
 
K

Knute Johnson

Turner said:
Interesting. This works. Could it really just be that I didn't thread
it? If so, what's blocking? As far as I can tell, there's no limit to
the number of lines allowed to be obtained in a single thread, is
there?

I think there is a limit but it has been a long time since I've played
with JavaSound. I'm sure that it is at least in the tens of
SourceDataLines on Windows. Probably more on Linux.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top