Copyright 2012-04-26 Werner Randelshofer

org.monte.media.mp3
Class MP3AudioInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by javax.sound.sampled.AudioInputStream
          extended by org.monte.media.mp3.MP3AudioInputStream
All Implemented Interfaces:
java.io.Closeable

public class MP3AudioInputStream
extends javax.sound.sampled.AudioInputStream

AudioInputStream adapter for MP3ElementaryInputStream.

Unlike a regular audio input stream, an MP3 audio input stream can have a variable frame size and can change its encoding method in mid-stream. Therefore method getFormat can return different values for each frame, and mark/reset is not supported, and method getFrameLength can not return the total number of frames in the stream.

Version:
1.0.1 2011-03-18 Method read(byte[],int,int) must not read multiple frames of different sizes.
1.0 2011-01-01 Created.
Author:
Werner Randelshofer

Field Summary
 
Fields inherited from class javax.sound.sampled.AudioInputStream
format, frameLength, framePos, frameSize
 
Constructor Summary
MP3AudioInputStream(java.io.File file)
          Creates an MP3AudioInputStream and reads the stream until the first frame is reached.
MP3AudioInputStream(java.io.InputStream in)
          Creates an MP3AudioInputStream and reads the stream until the first frame is reached.
 
Method Summary
 int available()
           
 void close()
           
 javax.sound.sampled.AudioFormat getFormat()
          Returns the format of the next frame.
 long getFrameLength()
          Returns -1 because we don't know how many frames the stream has.
 void mark(int readlimit)
           
 boolean markSupported()
           
 int read()
          Throws an IOException, because the frame size is greater than 1.
 int read(byte[] b, int off, int len)
          Reads some number of bytes from the audio input stream and stores them into the buffer array b.
 void reset()
           
 long skip(long n)
           
 
Methods inherited from class javax.sound.sampled.AudioInputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MP3AudioInputStream

public MP3AudioInputStream(java.io.File file)
                    throws java.io.IOException
Creates an MP3AudioInputStream and reads the stream until the first frame is reached.

Parameters:
file - A File.
Throws:
java.io.IOException - if the file does not contain an MP3 elementary stream.

MP3AudioInputStream

public MP3AudioInputStream(java.io.InputStream in)
                    throws java.io.IOException
Creates an MP3AudioInputStream and reads the stream until the first frame is reached.

Parameters:
in - An InputStream.
Throws:
java.io.IOException - if the stream does not contain an MP3 elementary stream.
Method Detail

available

public int available()
              throws java.io.IOException
Overrides:
available in class javax.sound.sampled.AudioInputStream
Throws:
java.io.IOException

getFormat

public javax.sound.sampled.AudioFormat getFormat()
Returns the format of the next frame. Returns null if the stream is not positioned inside a frame.

Overrides:
getFormat in class javax.sound.sampled.AudioInputStream

getFrameLength

public long getFrameLength()
Returns -1 because we don't know how many frames the stream has.

Overrides:
getFrameLength in class javax.sound.sampled.AudioInputStream

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Overrides:
close in class javax.sound.sampled.AudioInputStream
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Throws an IOException, because the frame size is greater than 1.

Overrides:
read in class javax.sound.sampled.AudioInputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads some number of bytes from the audio input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. This method will always read an integral number of frames. If the length of the array is not an integral number of frames, a maximum of b.length - (b.length % frameSize) bytes will be read.

Overrides:
read in class javax.sound.sampled.AudioInputStream
Returns:
Returns the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class javax.sound.sampled.AudioInputStream
Throws:
java.io.IOException

mark

public void mark(int readlimit)
Overrides:
mark in class javax.sound.sampled.AudioInputStream

markSupported

public boolean markSupported()
Overrides:
markSupported in class javax.sound.sampled.AudioInputStream

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class javax.sound.sampled.AudioInputStream
Throws:
java.io.IOException

Copyright 2012-04-26 Werner Randelshofer