1

Here is the code i am using.I have used Clip class to play a clip.Program has been compiled without any error and is running properly but i can't hear the sound.

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;


public class ClipTest {

public static void main(String[] args) throws Exception {


File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);


DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);


clip.addLineListener(new LineListener() {
  public void update(LineEvent event) {
    if (event.getType() == LineEvent.Type.STOP) {
      event.getLine().close();
      System.exit(0);
    }
  }
});


clip.start();
}
}      
user1678213
  • 121
  • 6

1 Answers1

0

I just try your code. i think your mistake was that your file ist empty or not loaded correctly

I just replace

File soundFile = new File("./1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

with

InputStream inRequest = this.getClass().getResourceAsStream("1.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

Here is the new class

public class ClipTest {

    public void run() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        InputStream inRequest = this.getClass().getResourceAsStream("batR.wav");
        AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest);

        DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open(sound);

        clip.addLineListener(new LineListener() {

            public void update(LineEvent event) {
                if(event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                    System.exit(0);
                }
            }
        });

        clip.start();

    }

    public static void main(String[] args) throws Exception {
        ClipTest clipTest = new ClipTest();
        clipTest.run();

    }
}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • no buddy,your code is not also working ....but finally i figured out the problem in my code...and your code has the same problem....i am posting the resolved version ....by the way thnx for your help – user1678213 Feb 09 '13 at 09:19
  • If your code is working on your machine then you might be using JDK 1.4.2 or earlier . – user1678213 Feb 09 '13 at 09:23
  • but i am using java 1.7 and your code isnt working in 1.7 bcoz In the JDK 1.7 all java sound threads have been changed to daemon threads so java sound thread doesn't stop main method from exiting and this cause your program to terminate after calling cliptest.run() bcoz after that main method gets terminated – user1678213 Feb 09 '13 at 09:38
  • [daemon threads](http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java) ...this will help you understand what i m saying – user1678213 Feb 09 '13 at 09:41
  • Dear friend, i'am sorry you didn't tell us which java version you are using. so i suppose you are using 1.6 which is the most used version. – Festus Tamakloe Feb 09 '13 at 09:43
  • oh sorry...thats my mistake...actully i am new to java as well as on stackoverflow... – user1678213 Feb 09 '13 at 09:50
  • i cant upvote you coz i dont have enough reputation...so (+1) from here – user1678213 Feb 09 '13 at 09:51