Java Source Code: __.si.SimpleSoundPlayer


   1: import java.io.*;
   2: import javax.sound.sampled.*;
   3: 
   4: /**
   5:     The SimpleSoundPlayer encapsulates a sound that can be opened
   6:     from the file system and later played.
   7: */
   8:	  public class SimpleSoundPlayer  {
   9:
  10:	      public static void main(String[] args) {
  11:        // load a sound
  12:        SimpleSoundPlayer sound =
  13:            new SimpleSoundPlayer("../sounds/voice.wav");
  14:
  15:        // create the stream to play
  16:        InputStream stream =
  17:            new ByteArrayInputStream(sound.getSamples());
  18:
  19:        // play the sound
  20:        sound.play(stream);
  21:
  22:        // exit
  23:        System.exit(0);
  24:    }
  25:
  26:    private AudioFormat format;
  27:    private byte[] samples;
  28:
  29:    /**
  30:        Opens a sound from a file.
  31:    */
  32:	      public SimpleSoundPlayer(String filename) {
  33:	          try {
  34:            // open the audio input stream
  35:            AudioInputStream stream =
  36:                AudioSystem.getAudioInputStream(
  37:                new File(filename));
  38:
  39:            format = stream.getFormat();
  40:
  41:            // get the audio samples
  42:            samples = getSamples(stream);
  43:        }
  44:	          catch (UnsupportedAudioFileException ex) {
  45:            ex.printStackTrace();
  46:        }
  47:	          catch (IOException ex) {
  48:            ex.printStackTrace();
  49:        }
  50:    }
  51:
  52:
  53:    /**
  54:        Gets the samples of this sound as a byte array.
  55:    */
  56:	      public byte[] getSamples() {
  57:        return samples;
  58:    }
  59:
  60:
  61:    /**
  62:        Gets the samples from an AudioInputStream as an array
  63:        of bytes.
  64:    */
  65:	      private byte[] getSamples(AudioInputStream audioStream) {
  66:        // get the number of bytes to read
  67:        int length = (int)(audioStream.getFrameLength() *
  68:            format.getFrameSize());
  69:
  70:        // read the entire stream
  71:        byte[] samples = new byte[length];
  72:        DataInputStream is = new DataInputStream(audioStream);
  73:	          try {
  74:            is.readFully(samples);
  75:        }
  76:	          catch (IOException ex) {
  77:            ex.printStackTrace();
  78:        }
  79:
  80:        // return the samples
  81:        return samples;
  82:    }
  83:
  84:
  85:    /**
  86:        Plays a stream. This method blocks (doesn't return) until
  87:        the sound is finished playing.
  88:    */
  89:	      public void play(InputStream source) {
  90:
  91:        // use a short, 100ms (1/10th sec) buffer for real-time
  92:        // change to the sound stream
  93:        int bufferSize = format.getFrameSize() *
  94:            Math.round(format.getSampleRate() / 10);
  95:        byte[] buffer = new byte[bufferSize];
  96:
  97:        // create a line to play to
  98:        SourceDataLine line;
  99:	          try {
 100:            DataLine.Info info =
 101:                new DataLine.Info(SourceDataLine.class, format);
 102:            line = (SourceDataLine)AudioSystem.getLine(info);
 103:            line.open(format, bufferSize);
 104:        }
 105:	          catch (LineUnavailableException ex) {
 106:            ex.printStackTrace();
 107:            return;
 108:        }
 109:
 110:        // start the line
 111:        line.start();
 112:
 113:        // copy data to the line
 114:	          try {
 115:            int numBytesRead = 0;
 116:	              while (numBytesRead != -1) {
 117:                numBytesRead =
 118:                    source.read(buffer, 0, buffer.length);
 119:	                  if (numBytesRead != -1) {
 120:                   line.write(buffer, 0, numBytesRead);
 121:                }
 122:            }
 123:        }
 124:	          catch (IOException ex) {
 125:            ex.printStackTrace();
 126:        }
 127:
 128:        // wait until all data is played, then close the line
 129:        line.drain();
 130:        line.close();
 131:
 132:    }
 133:
 134:}