javazoom.jlgui.basicplayer

Class BasicPlayer

Implemented Interfaces:
BasicController, Runnable

public class BasicPlayer
extends java.lang.Object
implements BasicController, 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 Summary

static int
EXTERNAL_BUFFER_SIZE
static int
OPENED
static int
PAUSED
static int
PLAYING
static int
SEEKING
static int
SKIP_INACCURACY_SIZE
static int
STOPPED
static int
UNKNOWN
These variables are used to distinguish stopped, paused, playing states.
protected int
encodedLength
protected AudioFileFormat
m_audioFileFormat
protected AudioInputStream
m_audioInputStream
protected Object
m_dataSource
protected AudioInputStream
m_encodedaudioInputStream
protected FloatControl
m_gainControl
protected SourceDataLine
m_line
protected String
m_mixerName
protected FloatControl
m_panControl
protected Thread
m_thread

Constructor Summary

BasicPlayer()
Constructs a Basic Player.

Method Summary

void
addBasicPlayerListener(BasicPlayerListener bpl)
Add listener to be notified.
protected void
closeStream()
protected void
createLine()
Inits a DateLine.
We check if the line supports Gain and Pan controls.
protected Map
deepCopy(Map src)
Deep copy of a Map.
protected int
getEncodedStreamPosition()
float
getGainValue()
Returns Gain value.
int
getLineBufferSize()
Return SourceDataLine buffer size.
int
getLineCurrentBufferSize()
Return SourceDataLine current buffer size.
Collection
getListeners()
Return registered listeners.
float
getMaximumGain()
Gets max Gain value.
float
getMinimumGain()
Gets min Gain value.
Mixer
getMixer(String name)
String
getMixerName()
List
getMixers()
float
getPan()
Returns Pan value.
float
getPrecision()
Returns Pan precision.
long
getSleepTime()
Return thread sleep time in milliseconds.
int
getStatus()
Returns BasicPlayer status.
boolean
hasGainControl()
Returns true if Gain control is supported.
boolean
hasPanControl()
Returns true if Pan control is supported.
protected void
initAudioInputStream()
Inits AudioInputStream and AudioFileFormat from the data source.
protected void
initAudioInputStream(File file)
Inits Audio ressources from file.
protected void
initAudioInputStream(InputStream inputStream)
Inits Audio ressources from InputStream.
protected void
initAudioInputStream(URL url)
Inits Audio ressources from URL.
protected void
initLine()
Inits Audio ressources from AudioSystem.
protected void
notifyEvent(int code, int position, double value, Object description)
Notify listeners about a BasicPlayerEvent.
void
open(File file)
Open file to play.
void
open(InputStream inputStream)
Open inputstream to play.
void
open(URL url)
Open URL to play.
protected void
openLine()
Opens the line.
void
pause()
protected void
pausePlayback()
Pauses the playback.
Player Status = PAUSED.
void
play()
void
removeBasicPlayerListener(BasicPlayerListener bpl)
Remove registered listener.
protected void
reset()
void
resume()
protected void
resumePlayback()
Resumes the playback.
Player Status = PLAYING.
void
run()
Main loop.
long
seek(long bytes)
void
setGain(double fGain)
Sets Gain value.
void
setLineBufferSize(int size)
Set SourceDataLine buffer size.
void
setMixerName(String name)
void
setPan(double fPan)
Sets Pan value.
void
setSleepTime(long time)
Set thread sleep time.
protected long
skipBytes(long bytes)
Skip bytes in the File inputstream.
protected void
startPlayback()
Starts playback.
void
stop()
protected void
stopPlayback()
Stops the playback.
Player Status = STOPPED.
Thread should free Audio ressources.

Field Details

EXTERNAL_BUFFER_SIZE

public static int EXTERNAL_BUFFER_SIZE

OPENED

public static final int OPENED
Field Value:
3

PAUSED

public static final int PAUSED
Field Value:
1

PLAYING

public static final int PLAYING
Field Value:
0

SEEKING

public static final int SEEKING
Field Value:
4

SKIP_INACCURACY_SIZE

public static int SKIP_INACCURACY_SIZE

STOPPED

public static final int STOPPED
Field Value:
2

UNKNOWN

public static final int UNKNOWN
These variables are used to distinguish stopped, paused, playing states. We need them to control Thread.
Field Value:
-1

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

Constructor Details

BasicPlayer

public BasicPlayer()
Constructs a Basic Player.

Method Details

addBasicPlayerListener

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

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.
Parameters:
src -
Returns:

getEncodedStreamPosition

protected int getEncodedStreamPosition()

getGainValue

public float getGainValue()
Returns Gain value.

getLineBufferSize

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

getLineCurrentBufferSize

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

getListeners

public Collection getListeners()
Return registered listeners.
Returns:

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.
Returns:
-1 means no sleep time.

getStatus

public int getStatus()
Returns BasicPlayer status.
Returns:
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.
Parameters:
code - event code.
position - in the stream when the event occurs.

open

public void open(File file)
            throws BasicPlayerException
Open file to play.
Specified by:
open in interface BasicController

open

public void open(InputStream inputStream)
            throws BasicPlayerException
Open inputstream to play.
Specified by:
open in interface BasicController

open

public void open(URL url)
            throws BasicPlayerException
Open URL to play.
Specified by:
open in interface BasicController

openLine

protected void openLine()
            throws LineUnavailableException
Opens the line.

pause

public void pause()
            throws BasicPlayerException
Specified by:
pause in interface BasicController

pausePlayback

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

play

public void play()
            throws BasicPlayerException
Specified by:
play in interface BasicController

removeBasicPlayerListener

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

reset

protected void reset()

resume

public void resume()
            throws BasicPlayerException
Specified by:
resume in interface BasicController

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.

seek

public long seek(long bytes)
            throws BasicPlayerException
Specified by:
seek in interface BasicController

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

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.

setMixerName

public void setMixerName(String name)

setPan

public void setPan(double fPan)
            throws BasicPlayerException
Sets Pan value. Line should be opened before calling this method. Linear scale : -1.0 <--> +1.0
Specified by:
setPan in interface BasicController

setSleepTime

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

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

startPlayback

protected void startPlayback()
            throws BasicPlayerException
Starts playback.

stop

public void stop()
            throws BasicPlayerException
Specified by:
stop in interface BasicController

stopPlayback

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

JavaZOOM 1999-2006