📜 ⬆️ ⬇️

Sound reproduction in Java

Introduction


There is simply no normal Russian-language information on the topic. Java-tutorials also leave much to be desired. And the architecture of javax.sound.sampled, though simple, is far from trivial. Therefore, I decided to dedicate my first post on Habré to this particular topic. Let's start:

Sound reproduction


It's all more or less simple. Import javax.sound.sampled and go:
try { File soundFile = new File("snd.wav"); //  // AudioInputStream //    IOException  UnsupportedAudioFileException AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); //   Clip //  LineUnavailableException Clip clip = AudioSystem.getClip(); //     Clip //  IOException  LineUnavailableException clip.open(ais); clip.setFramePosition(0); //    clip.start(); //!!! //    ,   ,     // GUI-  3    Thread.sleep(clip.getMicrosecondLength()/1000); clip.stop(); // clip.close(); // } catch (IOException | UnsupportedAudioFileException | LineUnavailableException exc) { exc.printStackTrace(); } catch (InterruptedException exc) {} 


Volume control

Having played with the sounds, you probably want to be able to programmatically change the volume of the sound. Java Sound API provides this feature with proprietary curves.
 //   FloatControl vc = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); //  //      vc.getMinimum()  vc.getMaximum() vc.setValue(5); //  

This code should be placed between the lines clip.open (ais) and clip.setFramePosition (0) .

Simplify the process


And finally, so that you do not suffer, I post the class for playing sounds
 import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.FloatControl; import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineListener; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; public class Sound implements AutoCloseable { private boolean released = false; private AudioInputStream stream = null; private Clip clip = null; private FloatControl volumeControl = null; private boolean playing = false; public Sound(File f) { try { stream = AudioSystem.getAudioInputStream(f); clip = AudioSystem.getClip(); clip.open(stream); clip.addLineListener(new Listener()); volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); released = true; } catch (IOException | UnsupportedAudioFileException | LineUnavailableException exc) { exc.printStackTrace(); released = false; close(); } } // true    , false    public boolean isReleased() { return released; } //       public boolean isPlaying() { return playing; } //  /* breakOld  ,      breakOld==true,            */ public void play(boolean breakOld) { if (released) { if (breakOld) { clip.stop(); clip.setFramePosition(0); clip.start(); playing = true; } else if (!isPlaying()) { clip.setFramePosition(0); clip.start(); playing = true; } } } //   ,   play(true) public void play() { play(true); } //   public void stop() { if (playing) { clip.stop(); } } public void close() { if (clip != null) clip.close(); if (stream != null) try { stream.close(); } catch (IOException exc) { exc.printStackTrace(); } } //   /* x      0  1 (     ) */ public void setVolume(float x) { if (x<0) x = 0; if (x>1) x = 1; float min = volumeControl.getMinimum(); float max = volumeControl.getMaximum(); volumeControl.setValue((max-min)*x+min); } //    (  0  1) public float getVolume() { float v = volumeControl.getValue(); float min = volumeControl.getMinimum(); float max = volumeControl.getMaximum(); return (v-min)/(max-min); } //     public void join() { if (!released) return; synchronized(clip) { try { while (playing) clip.wait(); } catch (InterruptedException exc) {} } } //  ,   public static Sound playSound(String path) { File f = new File(path); Sound snd = new Sound(f); snd.play(); return snd; } private class Listener implements LineListener { public void update(LineEvent ev) { if (ev.getType() == LineEvent.Type.STOP) { playing = false; synchronized(clip) { clip.notify(); } } } } } 

')
It is very easy to use, for example:
 Sound.playSound("sounds/hello.wav").join(); 


Formats


A few words about the support of audio file formats: forget about mp3 and remember wav. Also supports au and aif.

Source: https://habr.com/ru/post/191422/


All Articles