FileDocCategorySizeDatePackage
PCMFilePlayer.javaAPI DocExample3388Mon Jan 09 11:02:00 GMT 2006None

PCMFilePlayer

public class PCMFilePlayer extends Object implements Runnable

Fields Summary
File
file
AudioInputStream
in
SourceDataLine
line
int
frameSize
byte[]
buffer
Thread
playThread
boolean
playing
boolean
notYetEOF
Constructors Summary
public PCMFilePlayer(File f)

    
       
         
               
                
        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");
        line.open();
        System.out.println ("opened line");
        playThread = new Thread (this);
        playing = false;
        notYetEOF = true;
        playThread.start();
    
Methods Summary
public java.io.FilegetFile()

        return file;
    
public javax.sound.sampled.SourceDataLinegetLine()

        return line;
    
public voidrun()

        int readPoint = 0;
        int bytesRead = 0;
        
        try {
            while (notYetEOF) {
                if (playing) {
                    bytesRead = in.read (buffer,
                                         readPoint,
                                         buffer.length - readPoint);
                    if (bytesRead == -1) {
                        notYetEOF = false;
                        break;
                    }
                    // 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,
                                      leftover);
                    readPoint = leftover;
                    
                } else {
                    // if not playing
                    // Thread.yield();
                    try { Thread.sleep (10);} 
                    catch (InterruptedException ie) {}
                }
            } // while notYetEOF
            System.out.println ("reached eof");
            line.drain();
            line.stop();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            // line.close();
        }
    
public voidstart()

        playing = true;
        if (! playThread.isAlive())
            playThread.start();
        line.start();
    
public voidstop()

        playing = false;
        line.stop();