swing app using threads

G

GX

Hi all

I have a basic swing app that has a text area and 2 buttons (to keep
things simple), when clicking the start button a sound is played,
however this makes the app non responsive, all other controls do not
respond till the sound stops.

I made the class that plays the sound extend thread now the play
button calls the start() method then the playSound() method, but I
have the same result.

What is the correct way to implement this feature?
What should I look into?


Regards

GX
 
K

Knute Johnson

GX said:
Hi all

I have a basic swing app that has a text area and 2 buttons (to keep
things simple), when clicking the start button a sound is played,
however this makes the app non responsive, all other controls do not
respond till the sound stops.

I made the class that plays the sound extend thread now the play
button calls the start() method then the playSound() method, but I
have the same result.

Do you create a new Thread and call start on it?
What is the correct way to implement this feature?
What should I look into?


Regards

GX

If playSound() is the method that plays the audio, just wrap it in a
Runnable and start it with a new thread.

Runnable r = new Runnable() {
public void run() {
playSound();
}
};
new Thread(r).start();

And off you go!
 
G

GX

There are better ways to do threading in a Swing app.  Like using the
SwingWorker class.  In your particular example, even without a code
example, it sounds to me as though even though you are apparently
starting your thread, the work to play the sound is still done on the
EDT, because your button click listener is still calling the playSound()
method directly.

With a concise-but-complete code example, even more specific advice
could be offered.  In fact, with such a code example, it might even be
possible to help you avoid using a Thread altogether, by showing us why
playing a sound is blocking your EDT.  When I've used audio in my Java
program, I used the javax.sound.sampled.Clip interface, and the
implementation I'm using plays the audio in the background without
blocking the thread on which you started playback.

IMHO, it would be better to use such an asynchronous playback API,
rather than creating your own with an explicit use of a new thread.

Pete

Hi Pete

Thank you for your suggestion, Instead of extending runnable I extend
SwingWorker this helps and now it works as I need it to.

Reagrds

GX
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top