Class BasicPlayer

  • All Implemented Interfaces:
    java.lang.Runnable, BasicController

    public class BasicPlayer
    extends java.lang.Object
    implements BasicController, java.lang.Runnable
    BasicPlayer is a threaded simple player class based on JavaSound API. It has been successfully tested under J2SE 1.3.x, 1.4.x and 1.5.x.
    • Field Detail

      • EXTERNAL_BUFFER_SIZE

        public static int EXTERNAL_BUFFER_SIZE
      • SKIP_INACCURACY_SIZE

        public static int SKIP_INACCURACY_SIZE
      • m_thread

        protected java.lang.Thread m_thread
      • m_dataSource

        protected java.lang.Object m_dataSource
      • m_encodedaudioInputStream

        protected javax.sound.sampled.AudioInputStream m_encodedaudioInputStream
      • encodedLength

        protected int encodedLength
      • m_audioInputStream

        protected javax.sound.sampled.AudioInputStream m_audioInputStream
      • m_audioFileFormat

        protected javax.sound.sampled.AudioFileFormat m_audioFileFormat
      • m_line

        protected javax.sound.sampled.SourceDataLine m_line
      • m_gainControl

        protected javax.sound.sampled.FloatControl m_gainControl
      • m_panControl

        protected javax.sound.sampled.FloatControl m_panControl
      • m_mixerName

        protected java.lang.String m_mixerName
      • UNKNOWN

        public static final int UNKNOWN
        These variables are used to distinguish stopped, paused, playing states. We need them to control Thread.
        See Also:
        Constant Field Values
    • Constructor Detail

      • BasicPlayer

        public BasicPlayer()
        Constructs a Basic Player.
    • Method Detail

      • reset

        protected void reset()
      • addBasicPlayerListener

        public void addBasicPlayerListener​(BasicPlayerListener bpl)
        Add listener to be notified.
        Parameters:
        bpl -
      • getListeners

        public java.util.Collection getListeners()
        Return registered listeners.
        Returns:
      • removeBasicPlayerListener

        public void removeBasicPlayerListener​(BasicPlayerListener bpl)
        Remove registered listener.
        Parameters:
        bpl -
      • setLineBufferSize

        public void setLineBufferSize​(int size)
        Set SourceDataLine buffer size. It affects audio latency. (the delay between line.write(data) and real sound). Minimum value should be over 10000 bytes.
        Parameters:
        size - -1 means maximum buffer size available.
      • getLineBufferSize

        public int getLineBufferSize()
        Return SourceDataLine buffer size.
        Returns:
        -1 maximum buffer size.
      • getLineCurrentBufferSize

        public int getLineCurrentBufferSize()
        Return SourceDataLine current buffer size.
        Returns:
      • setSleepTime

        public void setSleepTime​(long time)
        Set thread sleep time. Default is -1 (no sleep time).
        Parameters:
        time - in milliseconds.
      • getSleepTime

        public long getSleepTime()
        Return thread sleep time in milliseconds.
        Returns:
        -1 means no sleep time.
      • getStatus

        public int getStatus()
        Returns BasicPlayer status.
        Returns:
        status
      • initAudioInputStream

        protected void initAudioInputStream​(java.io.File file)
                                     throws javax.sound.sampled.UnsupportedAudioFileException,
                                            java.io.IOException
        Inits Audio ressources from file.
        Throws:
        javax.sound.sampled.UnsupportedAudioFileException
        java.io.IOException
      • initAudioInputStream

        protected void initAudioInputStream​(java.net.URL url)
                                     throws javax.sound.sampled.UnsupportedAudioFileException,
                                            java.io.IOException
        Inits Audio ressources from URL.
        Throws:
        javax.sound.sampled.UnsupportedAudioFileException
        java.io.IOException
      • initAudioInputStream

        protected void initAudioInputStream​(java.io.InputStream inputStream)
                                     throws javax.sound.sampled.UnsupportedAudioFileException,
                                            java.io.IOException
        Inits Audio ressources from InputStream.
        Throws:
        javax.sound.sampled.UnsupportedAudioFileException
        java.io.IOException
      • initLine

        protected void initLine()
                         throws javax.sound.sampled.LineUnavailableException
        Inits Audio ressources from AudioSystem.
        Throws:
        javax.sound.sampled.LineUnavailableException
      • createLine

        protected void createLine()
                           throws javax.sound.sampled.LineUnavailableException
        Inits a DateLine.
        We check if the line supports Gain and Pan controls. From the AudioInputStream, i.e. from the sound file, we fetch information about the format of the audio data. These information include the sampling frequency, the number of channels and the size of the samples. There information are needed to ask JavaSound for a suitable output line for this audio file. Furthermore, we have to give JavaSound a hint about how big the internal buffer for the line should be. Here, we say AudioSystem.NOT_SPECIFIED, signaling that we don't care about the exact size. JavaSound will use some default value for the buffer size.
        Throws:
        javax.sound.sampled.LineUnavailableException
      • openLine

        protected void openLine()
                         throws javax.sound.sampled.LineUnavailableException
        Opens the line.
        Throws:
        javax.sound.sampled.LineUnavailableException
      • stopPlayback

        protected void stopPlayback()
        Stops the playback.
        Player Status = STOPPED.
        Thread should free Audio ressources.
      • pausePlayback

        protected void pausePlayback()
        Pauses the playback.
        Player Status = PAUSED.
      • resumePlayback

        protected void resumePlayback()
        Resumes the playback.
        Player Status = PLAYING.
      • run

        public void run()
        Main loop. Player Status == STOPPED || SEEKING => End of Thread + Freeing Audio Ressources.
        Player Status == PLAYING => Audio stream data sent to Audio line.
        Player Status == PAUSED => Waiting for another status.
        Specified by:
        run in interface java.lang.Runnable
      • skipBytes

        protected long skipBytes​(long bytes)
                          throws BasicPlayerException
        Skip bytes in the File inputstream. It will skip N frames matching to bytes, so it will never skip given bytes length exactly.
        Parameters:
        bytes -
        Returns:
        value>0 for File and value=0 for URL and InputStream
        Throws:
        BasicPlayerException
      • notifyEvent

        protected void notifyEvent​(int code,
                                   int position,
                                   double value,
                                   java.lang.Object description)
        Notify listeners about a BasicPlayerEvent.
        Parameters:
        code - event code.
        position - in the stream when the event occurs.
      • getEncodedStreamPosition

        protected int getEncodedStreamPosition()
      • closeStream

        protected void closeStream()
      • hasGainControl

        public boolean hasGainControl()
        Returns true if Gain control is supported.
      • getGainValue

        public float getGainValue()
        Returns Gain value.
      • getMaximumGain

        public float getMaximumGain()
        Gets max Gain value.
      • getMinimumGain

        public float getMinimumGain()
        Gets min Gain value.
      • hasPanControl

        public boolean hasPanControl()
        Returns true if Pan control is supported.
      • getPrecision

        public float getPrecision()
        Returns Pan precision.
      • getPan

        public float getPan()
        Returns Pan value.
      • deepCopy

        protected java.util.Map deepCopy​(java.util.Map src)
        Deep copy of a Map.
        Parameters:
        src -
        Returns:
      • setGain

        public void setGain​(double fGain)
                     throws BasicPlayerException
        Sets Gain value. Line should be opened before calling this method. Linear scale 0.0 <--> 1.0 Threshold Coef. : 1/2 to avoid saturation.
        Specified by:
        setGain in interface BasicController
        Parameters:
        fGain - value from 0.0 to 1.0
        Throws:
        BasicPlayerException
      • getMixers

        public java.util.List getMixers()
      • getMixer

        public javax.sound.sampled.Mixer getMixer​(java.lang.String name)
      • getMixerName

        public java.lang.String getMixerName()
      • setMixerName

        public void setMixerName​(java.lang.String name)