Name: yyT116575 Date: 11/28/2000
bash-2.02$ java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Run the following code. The "ringin.wav" that it accesses should be the one
found on Windows NT under c:\Program Files\PhoneTools (an 18K file), not the 10K
version that appears elsewhere. (I have seen this same problem occur with other
.wav files as well, but that particular one seems to produce it quickly and
reliably.)
Typically after about 10 rings, one or both threads hang in the drain method.
If this doesn't seem to be happening on your machine, please try with other .wav
files and/or with other Thread.sleep values in MyThread.run.
Of course drain may be an innocent bystander rather than the actual culprit.
import java.net.*;
import javax.sound.sampled.*;
class Test0
{
public static void main(String[] args)
{
try {
go();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private static void go() throws Exception
{
URL u5 = new URL("file:ringin.wav");
MyThread t1 = new MyThread(u5);
t1.start();
while (true)
{
showUs(u5);
}
}
private static class MyThread extends Thread
{
URL u;
MyThread(URL u)
{
this.u = u;
}
public void run(){
while (true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
try {
showUs(u);
} catch (Exception ex)
{
System.out.println("From MyThread:");
ex.printStackTrace();
}
}
}
}
private static int count = 0;
private static void showUs(URL u) throws Exception
{
System.out.println();
count++;
int mycount = count;
System.out.println("Trial number " + count);
System.out.println(u);
AudioFileFormat fformat =
AudioSystem.getAudioFileFormat(u);
AudioInputStream astream = AudioSystem.getAudioInputStream(u);
AudioFormat format = astream.getFormat();
System.out.println(format);
DataLine.Info sourceInfo =
new DataLine.Info(SourceDataLine.class, format);
System.out.println(sourceInfo);
SourceDataLine line = (SourceDataLine)AudioSystem.getLine(sourceInfo);
line.open(format);
line.start();
int fsize = format.getFrameSize();
byte[] buf = new byte[fsize];
while ( true )
{
int offset = 0;
int toRead = fsize;
int len = 0;
while ( toRead > 0 )
{
len = astream.read(buf, offset, toRead);
if ( len == 0 )
System.out.println("Read zero bytes");
if ( len == -1 )
break;
offset += len;
toRead -= len;
}
if ( len == -1 )
break;
line.write(buf, 0, fsize);
}
astream.close();
System.out.println("Draining " + mycount);
line.drain();
System.out.println("finished draining " + mycount);
line.stop();
line.close();
}
}
(Review ID: 112901)
======================================================================