Example usage for javax.sound.sampled AudioSystem getLine

List of usage examples for javax.sound.sampled AudioSystem getLine

Introduction

In this page you can find the example usage for javax.sound.sampled AudioSystem getLine.

Prototype

public static Line getLine(Line.Info info) throws LineUnavailableException 

Source Link

Document

Obtains a line that matches the description in the specified Line.Info object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DataLine.Info info = null;//from w  ww .  j  a v a2s . c  om
    Clip clip = (Clip) AudioSystem.getLine(info);
    double timeInSeconds = clip.getMicrosecondPosition() / 1000000.0d;
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DataLine.Info info = null;/*from  w  w w . j  a v a 2 s .  co  m*/
    Clip clip = (Clip) AudioSystem.getLine(info);
    double durationInSecs = clip.getBufferSize()
            / (clip.getFormat().getFrameSize() * clip.getFormat().getFrameRate());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DataLine.Info info = null;/*from w  ww .j  a  va  2s.  c o  m*/
    Clip clip = (Clip) AudioSystem.getLine(info);
    clip.start();

    clip.loop(Clip.LOOP_CONTINUOUSLY);

    int numberOfPlays = 3;
    clip.loop(numberOfPlays - 1);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DataLine.Info info = null;/*from   w  w w.java2 s  .c  o m*/
    Clip clip = (Clip) AudioSystem.getLine(info);

    clip.addLineListener(new LineListener() {
        public void update(LineEvent evt) {
            if (evt.getType() == LineEvent.Type.STOP) {
            }
        }
    });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DataLine.Info info = null;/*  ww  w . j a v  a  2s  .com*/
    Clip clip = (Clip) AudioSystem.getLine(info);

    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
    double gain = .5D; // number between 0 and 1 (loudest)
    float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
    gainControl.setValue(dB);

    BooleanControl muteControl = (BooleanControl) clip.getControl(BooleanControl.Type.MUTE);
    muteControl.setValue(true);

    muteControl.setValue(false);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    float sampleRate = 8000;
    int sampleSizeInBits = 8;
    int channels = 1;
    boolean signed = true;
    boolean bigEndian = true;
    final AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);/* w  w  w .j a v  a  2 s.c  o m*/
    line.start();
    Runnable runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {

                int count = line.read(buffer, 0, buffer.length);
                if (count > 0) {
                    out.write(buffer, 0, count);
                }

                out.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-1);
            }
        }
    };
    Thread captureThread = new Thread(runner);
    captureThread.start();

    byte audio[] = out.toByteArray();
    InputStream input = new ByteArrayInputStream(audio);
    final SourceDataLine line1 = (SourceDataLine) AudioSystem.getLine(info);
    final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
    line1.open(format);
    line1.start();

    runner = new Runnable() {
        int bufferSize = (int) format.getSampleRate() * format.getFrameSize();

        byte buffer[] = new byte[bufferSize];

        public void run() {
            try {
                int count;
                while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
                    if (count > 0) {
                        line1.write(buffer, 0, count);
                    }
                }
                line1.drain();
                line1.close();
            } catch (IOException e) {
                System.err.println("I/O problems: " + e);
                System.exit(-3);
            }
        }
    };
    Thread playThread = new Thread(runner);
    playThread.start();

}

From source file:ClipTest.java

public static void main(String[] args) throws Exception {

    // specify the sound to play
    // (assuming the sound can be played by the audio system)
    File soundFile = new File("../sounds/voice.wav");
    AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

    // load the sound into memory (a Clip)
    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    Clip clip = (Clip) AudioSystem.getLine(info);
    clip.open(sound);/*from  w  ww. ja v a  2  s .  c  o  m*/

    // due to bug in Java Sound, explicitly exit the VM when
    // the sound has stopped.
    clip.addLineListener(new LineListener() {
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                event.getLine().close();
                System.exit(0);
            }
        }
    });

    // play the sound clip
    clip.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));

    // From URL/*from w  w  w .  ja v a  2 s.c o m*/
    // stream = AudioSystem.getAudioInputStream(new URL(
    // "http://hostname/audiofile"));

    AudioFormat format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(),
                format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2,
                format.getFrameRate(), true); // big endian
        stream = AudioSystem.getAudioInputStream(format, stream);
    }

    DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
            ((int) stream.getFrameLength() * format.getFrameSize()));
    Clip clip = (Clip) AudioSystem.getLine(info);

    clip.open(stream);

    clip.start();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
    //    stream = AudioSystem.getAudioInputStream(new URL(
    //      "http://hostname/audiofile"));

    AudioFormat format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(),
                format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2,
                format.getFrameRate(), true); // big endian
        stream = AudioSystem.getAudioInputStream(format, stream);
    }// w  w w .  j  a  v  a2  s . c o  m

    SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(),
            ((int) stream.getFrameLength() * format.getFrameSize()));
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(stream.getFormat());
    line.start();

    int numRead = 0;
    byte[] buf = new byte[line.getBufferSize()];
    while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
        int offset = 0;
        while (offset < numRead) {
            offset += line.write(buf, offset, numRead - offset);
        }
    }
    line.drain();
    line.stop();
}

From source file:Main.java

public Main() throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);//  w  w w .ja  v  a2s .  c om
    soundFile = chooser.getSelectedFile();

    System.out.println("Playing " + soundFile.getName());

    Line.Info linfo = new Line.Info(Clip.class);
    Line line = AudioSystem.getLine(linfo);
    clip = (Clip) line;
    clip.addLineListener(this);
    AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
    clip.open(ais);
    clip.start();
}