Prev: add authorization header (user/password) before sendRedirect
Next: Call for Papers Reminder (extended): International MultiConference of Engineers and Computer Scientists IMECS 2010
From: GX on 7 Dec 2009 01:07 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
From: Peter Duniho on 7 Dec 2009 02:05 GX wrote: > 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? 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
From: Knute Johnson on 7 Dec 2009 02:33 GX wrote: > 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! -- Knute Johnson email s/nospam/knute2009/ -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service ------->>>>>>http://www.NewsDemon.com<<<<<<------ Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
From: GX on 8 Dec 2009 09:39
On Dec 7, 7:05 am, Peter Duniho <NpOeStPe...(a)NnOwSlPiAnMk.com> wrote: > GX wrote: > > 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? > > 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 |