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();
}
}
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();
}
}
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.