From 9c5ea6ec1d3e03d879cc9625476f15f64d496d10 Mon Sep 17 00:00:00 2001 From: Pazaz Date: Mon, 6 Feb 2023 21:06:54 -0500 Subject: [PATCH] Merge pull request #608 * fix: Remove Oracle JRE dependency for sound effects --- .../src/main/java/SoundPlayer.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/2006Scape Client/src/main/java/SoundPlayer.java b/2006Scape Client/src/main/java/SoundPlayer.java index 6de32891..7f201a81 100644 --- a/2006Scape Client/src/main/java/SoundPlayer.java +++ b/2006Scape Client/src/main/java/SoundPlayer.java @@ -1,23 +1,18 @@ +import java.io.IOException; import java.io.InputStream; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.Clip; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.FloatControl; -import sun.audio.AudioPlayer; //Note! If you see a compile error here, make sure you set your JDK to Java 8! +import javax.sound.sampled.*; public class SoundPlayer implements Runnable { private AudioInputStream stream; private DataLine.Info info; - private Clip sound; + private SourceDataLine sound; private InputStream soundStream; private Thread player; private int delay; private int soundLevel; - private InputStream arg0; public static int volume; /** @@ -42,22 +37,37 @@ public class SoundPlayer implements Runnable { */ public void run() { try { - AudioPlayer.player.start(arg0); stream = AudioSystem.getAudioInputStream(soundStream); - info = new DataLine.Info(Clip.class, stream.getFormat()); - sound = (Clip) AudioSystem.getLine(info); - sound.open(stream); + + AudioFormat format = stream.getFormat(); + info = new DataLine.Info(SourceDataLine.class, format); + sound = (SourceDataLine) AudioSystem.getLine(info); + sound.open(format); + FloatControl volume = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN); volume.setValue(getDecibels(soundLevel - getVolume())); if (delay > 0) { Thread.sleep(delay); } sound.start(); - while (sound.isActive()) { - Thread.sleep(250); + + int nBytesRead = 0; + int EXTERNAL_BUFFER_SIZE = 524288; + byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; + try { + while (nBytesRead != -1) { + nBytesRead = stream.read(abData, 0, abData.length); + if (nBytesRead >= 0) { + sound.write(abData, 0, nBytesRead); + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + sound.drain(); + sound.close(); } - Thread.sleep(10000); - sound.close(); + stream.close(); player.interrupt(); } catch (Exception e) {