From 2ef0c291a174cf7bafd9f49b343e3eedbf5607b4 Mon Sep 17 00:00:00 2001 From: dsidan Date: Wed, 4 May 2022 17:59:29 -0400 Subject: [PATCH] Adding method that could play a sound file. No external libraries used. Issue - https://github.com/iluwatar/30-seconds-of-java/issues/108 --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 14a03f22..92bb9d1b 100644 --- a/README.md +++ b/README.md @@ -583,6 +583,33 @@ public static int calculateLuhnChecksum(long num) { } ``` +### Play sound + +```java + public static void playSound(String soundFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException { + File f = new File("./" + soundFile); + AudioInputStream audioIn = null; + Clip clip = null; + try { + audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL()); + clip = AudioSystem.getClip(); + clip.open(audioIn); + clip.start(); + FloatControl volume = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN); + volume.setValue(1.0f); // Reduce volume by 10 decibels. + clip.drain(); + } + finally { + try { + clip.close(); + } + catch (Exception exp) { + exp.printStackTrace(); + } + } + } +``` + ## Networking ### HTTP GET