Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java sound problem

Reply
Thread Tools

java sound problem

 
 
Gergely Buday
Guest
Posts: n/a
 
      06-04-2004
Hi Folks,

I'm having a problem. My SourceDataLine does not send a STOP event when it
finishes. My ugly hack is the following (see the while(true) cycle in
Player.run()):

public class SourceDatalineSoundPlayer implements LineListener,
SoundFilePlayerInterface {
private Player player = null;
private AudioInputStream ais = null;
private AudioFormat audioFormat = null;
private DataLine.Info info = null;
private SourceDataLine line = null;
private String filename;
private NavigationControl navCtrl = null;


private class Player implements Runnable {
Thread thread = null;

public void start() {
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}

public void stop() {
thread = null;
}

public void run() {
int bufferLengthInBytes = line.getBufferSize();
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead = 0;
line.start();
while (thread != null) {
try {
if ((numBytesRead = ais.read(data)) == -1) {
if (Constants.getDebug()) {
System.out.println("vége");
}
break;
}
int numBytesRemaining = numBytesRead;
while (numBytesRemaining > 0 ) {
numBytesRemaining -= line.write(data, 0, numBytesRemaining);
if (Constants.getDebug()) {
System.err.println("numBytesRemaining=" + numBytesRemaining);
}
}
} catch (Exception e) {
break;
} // try - catch
} // while (thread != null)

while (true) {
if (Constants.getDebug()) {
System.out.println("line.isActive()=" + line.isActive());
}
if (!line.isActive()) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}

} // while (true)

line.stop();
} // public void run()

public boolean loadAudioInputStream(AudioInputStream stream) {

AudioFormat tempAudioFormat = stream.getFormat();
SourceDataLine tempLine = null;
DataLine.Info tempInfo =
new DataLine.Info(SourceDataLine.class, tempAudioFormat);
try {
tempLine = (SourceDataLine) AudioSystem.getLine(tempInfo);
tempLine.open(tempAudioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.err.println("Line is not available in loadAudioInputStream");
return false;
}

ais = stream;
ais.mark(20000000);
audioFormat = tempAudioFormat;
info = tempInfo;
line = tempLine;
line.addLineListener(this);
return true;
}

public void update(LineEvent event) {
if (Constants.getDebug()) {
System.out.println("event = " + event.toString());
}
}


If I remove line.stop(), the output is the following:

event = Start event from line com.sun.media.sound.MixerSourceLine@9a9b65
numBytesRemaining=0
numBytesRemaining=0
vége ["the end"]
line.isActive()=true
line.isActive()=false

so it does not generate a STOP event (the above update method do receive the
START event, as you can see from the output).

I get "STOP" event only if I use line.stop(), which is in contradiction with
DataLine documentation, what says

"A data line produces START and STOP events whenever it begins or ceases
active presentation or capture of data."

To my understanding, line should generate a STOP event on having no more
data input, shouldn't it?

Do I misunderstand the docs?

- Gergely



 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      06-05-2004
Gergely Buday wrote:

> Hi Folks,
>
> so it does not generate a STOP event (the above update method do receive the
> START event, as you can see from the output).
>
> I get "STOP" event only if I use line.stop(), which is in contradiction with
> DataLine documentation, what says
>
> "A data line produces START and STOP events whenever it begins or ceases
> active presentation or capture of data."
>
> To my understanding, line should generate a STOP event on having no more
> data input, shouldn't it?
>
> Do I misunderstand the docs?
>
> - Gergely


No. With a SourceDataLine you are only going to get a stop event when
you call stop on the line. A Clip will generate a stop event when all
of the clip's data has been presented. A Clip may be what you want here
too.

--

knute
s/nospam/knute/
 
Reply With Quote
 
 
 
 
Gergely Buday
Guest
Posts: n/a
 
      06-07-2004
Knute Johnson wrote:

> > To my understanding, [SourceDataLine] line should generate a STOP event

on having no more
> > data input, shouldn't it?
> >
> > Do I misunderstand the docs?
> >
> > - Gergely

>
> No. With a SourceDataLine you are only going to get a stop event when
> you call stop on the line. A Clip will generate a stop event when all
> of the clip's data has been presented. A Clip may be what you want here
> too.


My problem with Clip is that it does not allow me to play long files. I can
set the buffer size, one might remark, yes, but I cannot set a buffer size
greater than 4M, and for some reasons it allows me to play wav files of size
less than 2M only. And I have a lot of files longer than that.

- Gergely


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Playing sound through non default sound card dkga_dkga@yahoo.com Java 0 03-05-2006 04:38 AM
Sound Quality In Sound API Luc The Perverse Java 4 10-31-2005 04:36 AM
Sound card problem : Creative sound blaster live Marco Bakker Computer Support 2 08-29-2004 02:09 AM
No sound. Yellow exclamation by" Windows sound system". =?Utf-8?B?VGFueWE=?= MCSE 2 10-26-2003 07:44 PM
Geforce 4 MX 440 Sound Prob (thats right, sound) Matthew Morrison Computer Support 2 07-29-2003 01:23 AM



Advertisments