/* * Return data as a byte array. */ private static byte[] readByte(String filename) { byte[] data = null; AudioInputStream ais = null; try { // try to read from file File file = new File(filename); if (file.exists()) { ais = AudioSystem.getAudioInputStream(file); data = new byte[ais.available()]; ais.read(data); } // try to read from URL else { URL url = StdAudio.class.getResource(filename); ais = AudioSystem.getAudioInputStream(url); data = new byte[ais.available()]; ais.read(data); } } catch (Exception e) { System.out.println(e.getMessage()); throw new RuntimeException("Could not read " + filename); } return data; }
private void soundAbspielen(File sound) { if (!läuft) return; try { audioInputStream = AudioSystem.getAudioInputStream(sound); af = audioInputStream.getFormat(); size = (int) (af.getFrameSize() * audioInputStream.getFrameLength()); audio = new byte[size]; info = new DataLine.Info(Clip.class, af, size); audioInputStream.read(audio, 0, size); clip = (Clip) AudioSystem.getLine(info); clip.open(af, audio, 0, size); clip.start(); } catch (Exception e) { e.printStackTrace(); } }