1

So I have this code which is supposed to play an audio-file. It doesn't, despite compiling just fine and running without errors. FYI: file is in source-folder/speakers are on.

import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class SoundCheck {
public static void main(String[] args) {

    File song = new File("test.WAV");
    PlaySound(song);
}
static void PlaySound(File Sound) {
    try {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(Sound));
        clip.start();

        Thread.sleep(clip.getMicrosecondLength()/1000);
    }
    catch (Exception e){
    }               
}
}
BigJ4vage
  • 11
  • 2
  • 1
    Are there any exceptions thrown? (Swallowing them is probably a bad idea, especially if it doesn't work like you expect). – Andy Turner Oct 21 '15 at 21:19
  • What Andy is trying to say: you can't make the statement "running without errors" ... when the part that would be showing errors (your catch clause) is completely empty. Instead, you want at least some e.printStackTrace() there! And fr the real problem - you checked that you can play and hear exactly that file before? (you wouldnt be the first person to having speakers muted while testing audio output ) – GhostCat Oct 21 '15 at 21:23
  • @Jägermeister Thanks for the quick reply. I'm quite new to java and exception handling in particular. So I addded your line to the catch block and it returned some errors: – BigJ4vage Oct 21 '15 at 21:31
  • @Jägermeister javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 24 bit, stereo, 6 bytes/frame, little-endian not supported. at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:513) at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1304) at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:121) at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1085) – BigJ4vage Oct 21 '15 at 21:33
  • at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1175) at SoundCheck.PlaySound(SoundCheck.java:14) – BigJ4vage Oct 21 '15 at 21:33
  • Don't post this in comments. Noone can read it there. Check out this answer, it has something to do with the quality of your sound file http://stackoverflow.com/a/15857190/4130107 – Tobi Oct 21 '15 at 22:19
  • Thanks and sorry. So it has a 16-bit cap. – BigJ4vage Oct 21 '15 at 23:49

0 Answers0