1

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 ?

jimmy
  • 11
  • 3

1 Answers1

1

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.

Hal
  • 591
  • 4
  • 10
  • 28
  • C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:36: cannot find symbol symbol: class Clip Clip clickClip = AudioSystem.getClip(); ^ C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:36: cannot find symbol symbol: variable AudioSystem Clip clickClip = AudioSystem.getClip(); ^ C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:37: cannot find symbol symbol: class URL URL clipURL = new URL("file://C:/1.wav"); ^ – jimmy Oct 29 '10 at 07:31
  • C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:37: cannot find symbol symbol: class URL URL clipURL = new URL("file://C:/1.wav"); ^ C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:38: cannot find symbol symbol: class AudioInputStream AudioInputStream ais = AudioSystem.getAudioInputStream(clipURL); ^ C:\Documents and Settings\Jacky\My Documents\IamgeFrameTest.java:38: cannot find symbol symbol: variable AudioSystem AudioInputStream ais = AudioSystem.getAudioInputStream(clipURL); ^ 6 errors – jimmy Oct 29 '10 at 07:36
  • It's normal, these classes must be imported. Don't forget to add the following imports to your code: import java.net.*;import javax.sound.sampled.*; It should work now. When you have such errors, you should check javadoc here : http://download.oracle.com/javase/1.5.0/docs/api/index.html – Hal Oct 29 '10 at 07:41
  • unreported exception javax.sound.sampled.LineUnavailableException; must be caught or declared to be thrown Clip clickClip = AudioSystem.getClip(); ^ unreported exception java.net.MalformedURLException; must be caught or declared to be thrown URL clipURL = new URL("file://C:/1.wav"); ^ – jimmy Oct 29 '10 at 07:51
  • unreported exception javax.sound.sampled.UnsupportedAudioFileException; must be caught or declared to be thrown AudioInputStream ais = AudioSystem.getAudioInputStream(clipURL); ^ unreported exception javax.sound.sampled.LineUnavailableException; must be caught or declared to be thrown clickClip.open(ais); ^ 4 errors – jimmy Oct 29 '10 at 07:51
  • I've imported import java.net.*; import javax.sound.sampled.*; – jimmy Oct 29 '10 at 07:52
  • all I'm importing now is import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import java.net.*; import javax.sound.sampled.*; – jimmy Oct 29 '10 at 07:53
  • For the Exception issue, check the edit I've made on my post. – Hal Oct 29 '10 at 08:34
  • can u plz check this ? I've uploded the code i want u to add ur poart and make it run can u ? – jimmy Oct 29 '10 at 09:11
  • If you have other errors, you sometimes have to do things by yourself. The problem about playing an audio file looks resolved and thus you should mark this answer as resolved. Otherwise, post the errors you get. I'm not going to make your work buddy. – Hal Oct 29 '10 at 09:11