简体   繁体   中英

playing a sound

I'm very beginner in Java, so ...

I've written a simple Java code to display images from my hard drive wherever I click the mouse, not on the applet, on panel, now how can I make a sound play automatically when I view 6 pictures ?

public void mouseClicked(MouseEvent e) { 
            if (count == images.length - 1) { 
                    ???????????????????????
            } else { 
                    count++; 
            } 
            x = e.getX(); 
            y = e.getY(); 
            frameTest.repaint(); 
            } 

I want to play a sound file from the Hard drive, in the place of question marks ..

can some one help plz ?

Try to write this inside your if :

try
{
     Clip clickClip = AudioSystem.getClip();
     URL clipURL = new URL("file://C:/aFile.wav");
     AudioInputStream ais = AudioSystem.getAudioInputStream(clipURL);
     clickClip.open(ais);
     clickClip.start();
}
catch(Exception e)
{
     System.out.ptintln("Something didn't work !\n" + e.printStackTrace());
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM