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.
EXTERNAL_BUFFER_SIZE
public static int EXTERNAL_BUFFER_SIZE
OPENED
public static final int OPENED
PAUSED
public static final int PAUSED
PLAYING
public static final int PLAYING
SEEKING
public static final int SEEKING
SKIP_INACCURACY_SIZE
public static int SKIP_INACCURACY_SIZE
STOPPED
public static final int STOPPED
UNKNOWN
public static final int UNKNOWN
These variables are used to distinguish stopped, paused, playing states.
We need them to control Thread.
encodedLength
protected int encodedLength
m_audioFileFormat
protected AudioFileFormat m_audioFileFormat
m_audioInputStream
protected AudioInputStream m_audioInputStream
m_dataSource
protected Object m_dataSource
m_encodedaudioInputStream
protected AudioInputStream m_encodedaudioInputStream
m_gainControl
protected FloatControl m_gainControl
m_line
protected SourceDataLine m_line
m_mixerName
protected String m_mixerName
m_panControl
protected FloatControl m_panControl
m_thread
protected Thread m_thread
addBasicPlayerListener
public void addBasicPlayerListener(BasicPlayerListener bpl)
Add listener to be notified.
closeStream
protected void closeStream()
createLine
protected void createLine()
throws 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.
deepCopy
protected Map deepCopy(Map src)
Deep copy of a Map.
getEncodedStreamPosition
protected int getEncodedStreamPosition()
getGainValue
public float getGainValue()
Returns Gain value.
getLineBufferSize
public int getLineBufferSize()
Return SourceDataLine buffer size.
getLineCurrentBufferSize
public int getLineCurrentBufferSize()
Return SourceDataLine current buffer size.
getListeners
public Collection getListeners()
Return registered listeners.
getMaximumGain
public float getMaximumGain()
Gets max Gain value.
getMinimumGain
public float getMinimumGain()
Gets min Gain value.
getMixer
public Mixer getMixer(String name)
getMixerName
public String getMixerName()
getMixers
public List getMixers()
getPan
public float getPan()
Returns Pan value.
getPrecision
public float getPrecision()
Returns Pan precision.
getSleepTime
public long getSleepTime()
Return thread sleep time in milliseconds.
getStatus
public int getStatus()
Returns BasicPlayer status.
hasGainControl
public boolean hasGainControl()
Returns true if Gain control is supported.
hasPanControl
public boolean hasPanControl()
Returns true if Pan control is supported.
initAudioInputStream
protected void initAudioInputStream()
throws BasicPlayerException
Inits AudioInputStream and AudioFileFormat from the data source.
initAudioInputStream
protected void initAudioInputStream(File file)
throws UnsupportedAudioFileException,
IOException
Inits Audio ressources from file.
initAudioInputStream
protected void initAudioInputStream(InputStream inputStream)
throws UnsupportedAudioFileException,
IOException
Inits Audio ressources from InputStream.
initAudioInputStream
protected void initAudioInputStream(URL url)
throws UnsupportedAudioFileException,
IOException
Inits Audio ressources from URL.
initLine
protected void initLine()
throws LineUnavailableException
Inits Audio ressources from AudioSystem.
notifyEvent
protected void notifyEvent(int code,
int position,
double value,
Object description)
Notify listeners about a BasicPlayerEvent.
code
- event code.position
- in the stream when the event occurs.
openLine
protected void openLine()
throws LineUnavailableException
Opens the line.
pausePlayback
protected void pausePlayback()
Pauses the playback.
Player Status = PAUSED.
removeBasicPlayerListener
public void removeBasicPlayerListener(BasicPlayerListener bpl)
Remove registered listener.
reset
protected void reset()
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.
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.
- setGain in interface BasicController
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.
size
- -1 means maximum buffer size available.
setMixerName
public void setMixerName(String name)
setSleepTime
public void setSleepTime(long time)
Set thread sleep time.
Default is -1 (no sleep time).
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.
- value>0 for File and value=0 for URL and InputStream
stopPlayback
protected void stopPlayback()
Stops the playback.
Player Status = STOPPED.
Thread should free Audio ressources.