Capturing Audio with Java Sound API : Audio « Development « Java Tutorial






import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class Main {
  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);
    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();

  }
}








6.50.Audio
6.50.1.Determining When a Sampled Audio Player Has Finished Playing
6.50.2.Setting the Volume of a Sampled Audio Player
6.50.3.Determining the Position of a Sampled Audio Player
6.50.4.Determining the Duration of a Sampled Audio File
6.50.5.Playing Streaming Sampled Audio
6.50.6.Loading and Playing Sampled Audio
6.50.7.Play an audio file from a JAR file
6.50.8.Determining the Encoding of a Sampled Audio File
6.50.9.Determining the File Format of a Sampled Audio File
6.50.10.Load image and sound from Jar file
6.50.11.A simple player for sampled sound files.
6.50.12.This is a simple program to record sounds and play them back
6.50.13.Capturing Audio with Java Sound API
6.50.14.Float Control Component
6.50.15.Make your own Java Media Player to play media files