1

I need help playing music in my java code. Right now I'm using the mouseClicked method in my JApplet, and each time I click on a certain location music, from a wav file is supposed to play. But when I run the applet, only the first object I click on will play, after nothing else will play and I cannot exit the applet (I have to force quit)

I have already tried using the clip.stop() method after it has played, and after using that no sound plays at all.

this is my code in my class for playing music that's attached to my JApplet.

public void playMusic() 

{

try{

File file = new File("island_music_x.wav");

        Clip clip = AudioSystem.getClip();

        clip.open(AudioSystem.getAudioInputStream(file));
        clip.start();
        Thread.sleep(clip.getMicrosecondLength());
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    } 
 }

this is code for an object that is supposed to be clicked in my applet to play music.

xpos5 = me.getX();

ypos5 = me.getY();

if

(xpos5 > rect5xco && xpos5 < rect5xco+rect5width && ypos5 > 
    rect5yco && ypos5 < rect5yco+rect5height)

rect5Clicked = true;

else

rect5Clicked = false;

when it is set to true I have code to play the music

if
(rect5Clicked == true)
 {
     try{
     m5.playMusic5();}
     catch (Exception e)
     {}
 }

When I click on an object, music is supposed to play, which occurs, however; I should be able to click on other objects after the first one and play music as well. But, the music only plays the first time. I am also unable to exit the appletviewer. Any help would be great thank you!!

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Astha N
  • 11
  • 1
  • 1
    Applet? Why would you want to create one of these to begin with? They're woefully out of date and not supported – Hovercraft Full Of Eels Apr 22 '19 at 23:07
  • It's for a class project and we have to use applets. – Astha N Apr 22 '19 at 23:11
  • As stated, applets are passe and a security threat and very few browsers support them Why not just find a widget that plays audio clips that you can embed in your web page? – WJS Apr 22 '19 at 23:11
  • I'm using BlueJ to run my applet and I'm not sure how to run the program through a webpage instead of appletviewer – Astha N Apr 22 '19 at 23:15
  • 1
    And you can't/shouldn't be trying to load resources from the local disk, this will fail. They either need to come from the Jar itself or the website which is hosting the a applet ... yet more reasons to avoid applets when you're just trying to learn – MadProgrammer Apr 22 '19 at 23:15
  • *"'m not sure how to run the program through a webpage instead of appletviewer"* since the applet plugin is deprecated and pretty much all browser now actively block it, I think you have larger problems at hand – MadProgrammer Apr 22 '19 at 23:16
  • Well, it appears that you are playing the clip inside the event handler (and thus, the EDT). If so, your applet will not be responsive to any other events. Have you discussed threads in your class? – WJS Apr 22 '19 at 23:16
  • 1
    `Thread.sleep(clip.getMicrosecondLength());` is a bad idea and should avoid doing so, there are better ways to determine when the clip stops playing – MadProgrammer Apr 22 '19 at 23:17
  • 1
    @jim829 `Clip` uses it's own `Thread`, but the `Thread.sleep`, well, that's a problem – MadProgrammer Apr 22 '19 at 23:17
  • @MadProgrammer. Ok. Didn't know that. – WJS Apr 22 '19 at 23:18
  • No, we haven't discussed threads yet. How do I play the clip outside of the event handler? Also, if I take the Thread.sleep(clip.getMicrosecondLength()); out no music will play, what should I replace it with? – Astha N Apr 22 '19 at 23:20
  • *"if I take the Thread.sleep(clip.getMicrosecondLength()); out no music will play"* For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Ignore the other stuff about threads, as stated by @MadProgrammer a `Clip` has its own thread & don't freeze the GUI with `sleep`. `catch (Exception e) {}` Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` – Andrew Thompson Apr 23 '19 at 16:57
  • @AsthaN [This](https://stackoverflow.com/questions/29836255/playing-multiple-sound-clips-using-clip-objects/29836764#29836764), [this](https://stackoverflow.com/questions/24274997/java-wav-player-adding-pause-and-continue/24275168#24275168), [this](https://stackoverflow.com/questions/42171398/actions-stop-working-in-java/42171625#42171625), [this](https://stackoverflow.com/questions/24600159/change-listener-creates-hinderance-in-jslider/24601589#24601589), [this](https://stackoverflow.com/questions/49547408/how-to-set-a-jslider-to-the-duration-of-an-audio-file/49548331#49548331) – MadProgrammer Apr 23 '19 at 21:47
  • @AsthaN Would suggest that you are doing something wrong which is not presented in the out-of-context code which you've presented – MadProgrammer Apr 23 '19 at 21:48

0 Answers0