Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swing problem

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
Right, here is what I want to do..
On the click of a button I want to
1) Change text diapleyd in JLabel called myLabel
2) Play some busin
3) Change text diapleyd in JLabel called myLabel

Now, here goes my ActionListener code,
Code:
class ButtonListener extends ActionListener
{
private JLabel myLabel;

public ButtonListener (JLabel myLabel)
{
    this.myLabel= myLabel;
}
public void actionPerformed(ActionEvent e)
{
    myLabel.setLabel("Playing");
    myLabel.repaint();
    //When button is clicked, play the music.wav file
    under the audio directory
    URL audioURL     = new URL("file:"
    +System.getProperty("user.dir") + "/audio/music.wav");
    AudioClip myClip = Applet.newAudioClip(audioURL);
    myClip.play();
    myLabel.setLabel("done");
    myLabel.repaint();
}

Where myLabel is 'newed' and added to a JPanel which uses FlowLayout. The JButton which uses this ButtonListener also resides within the same JPanel.

Problem
Upon Clicking the button, all I get is the music and the JLabel changed to "done" on first click!!

What's wrong. My music.wav lasted 10seconds!
 
Maybe Java starts a thread to run your wavefile, in which case it will execute at the same time as it continues with the main process. Not sure how Java deals with this as i thought you had to explicitly invoke threads. I am not sure.

By the way, are you learning to type?? just wondered!!!
 
could be soemthing to do with this?

"Unfortunately, the Java 2 SDK does not provide notification of when playback has completed. The Java Sound API provides this feature. It is available as an early access release. If you must use the SDK, you could try to determine the length of each clip and sleep for the same about of time before playing the next clip."

Taken from Sun help pages
 
Pipk,

No offence but the "comments" you have taken from Sun help pages just mentioned that Java 2 SDK is unable to determind the length of a sound clip.

However, I think your first post is correct and that is a Thread is created for each audioclip. This make more sense as there is no reason for a normal program to wait for a sound to complete playback so if you want to do so, you will have to call your program to 'sleep'.

If you take a look at the Java API for AudioClip interface, you can see that they mentioned that you can play multiple AudioClips at the same time so if you have only 1 thread, you can't possibily do so :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
I am not trying to play two clip
simply on click of a button
1) Change a JLabel to display "Playing"
2) Play the audio clip (lasts 10 second)
3) Change JLabel of 1) to "Done"

However, all I see is Done, and could not see 'Playing' whilst musing is playing.

Are you saying that JDK is 'automatically' multi-threading the rending of JLabel and the playing of audio clip??
 
Yep, that's what they're saying...

What your code is actually doing is 1) changing the button text, 2) STARTING the wave file which continues to play while the third step is happening, and 3) changing the button text before you ever see the first button text.

It looks like you have 2 options:
1) sleep for 10 seconds after starting the wave file (works if you know how long the particular file is going to last) before you change the text the second time.
2) use the Java Sound API, which will notify you when the sound is finished so you can change the text for the second time at that point.

Rose/Miros



Rosalie Dieteman
 
Yep, Miros is right. If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top