Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...

PCMFilePlayer Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!


import javax.sound.sampled.*;
import java.io.*;

public class PCMFilePlayer implements Runnable {
    File file;
    AudioInputStream in;
    SourceDataLine line;
    int frameSize;
    byte[] buffer = new byte [32 * 1024]; // 32k is arbitrary
    Thread playThread;
    boolean playing;
    boolean notYetEOF;

    public PCMFilePlayer (File f)
        throws IOException,
               LineUnavailableException {
        file = f;
        in = AudioSystem.getAudioInputStream (f);
        AudioFormat format = in.getFormat();
        AudioFormat.Encoding formatEncoding = format.getEncoding();
        if (! (formatEncoding.equals (AudioFormat.Encoding.PCM_SIGNED) ||
               formatEncoding.equals (AudioFormat.Encoding.PCM_UNSIGNED)))
            throw new UnsupportedAudioFileException (
                                                     file.getName() + " is not PCM audio");
        System.out.println ("got PCM format");
        frameSize = format.getFrameSize();
        DataLine.Info info =
            new DataLine.Info (SourceDataLine.class, format);
        System.out.println ("got info");
        line = (SourceDataLine) AudioSystem.getLine (info);
        System.out.println ("got line");
        System.out.println ("opened line");
        playThread = new Thread (this);
        playing = false;
        notYetEOF = true;

    public void run() {
        int readPoint = 0;
        int bytesRead = 0;

        try {
            while (notYetEOF) {
                if (playing) {
                    bytesRead = in.read (buffer,
                                         buffer.length - readPoint);
                    if (bytesRead == -1) {
                        notYetEOF = false;
                    // how many frames did we get,
                    // and how many are left over?
                    int frames = bytesRead / frameSize;
                    int leftover = bytesRead % frameSize;
                    // send to line
                    line.write (buffer, readPoint, bytesRead-leftover);
                    // save the leftover bytes
                    System.arraycopy (buffer, bytesRead,
                                      buffer, 0,
                    readPoint = leftover;

                } else {
                    // if not playing
                    // Thread.yield();
                    try { Thread.sleep (10);}
                    catch (InterruptedException ie) {}
            } // while notYetEOF
            System.out.println ("reached eof");
        } catch (IOException ioe) {
        } finally {
            // line.close();
    } // run

    public void start() {
        playing = true;
        if (! playThread.isAlive())

    public void stop() {
        playing = false;

    public SourceDataLine getLine() {
        return line;

    public File getFile() {
        return file;


/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/



The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

[ Go Back ]

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy