Play back stored audio - Java javax.sound.sampled

Java examples for javax.sound.sampled:Audio

Description

Play back stored audio

Demo Code


//package com.java2s;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class Main {
    /**//  w  w  w.  java2 s. c o  m
     * Play back stored audio
     * 
     * @param format
     *            the format
     * @param bytes
     *            byte array with the contents
     * @throws LineUnavailableException
     *             if there is a problem opening the audio line for play back.
     */
    public static void playBack(AudioFormat format, byte[] bytes)
            throws LineUnavailableException {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine audioLine = (SourceDataLine) AudioSystem
                .getLine(info);
        audioLine.open(format);
        audioLine.start();
        audioLine.write(bytes, 0, bytes.length);
        audioLine.drain();
        audioLine.close();

    }
}

Related Tutorials