Package javazoom.jlgui.basicplayer
Class BasicPlayer
- java.lang.Object
-
- javazoom.jlgui.basicplayer.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 Summary
Fields Modifier and Type Field Description protected int
encodedLength
static int
EXTERNAL_BUFFER_SIZE
protected javax.sound.sampled.AudioFileFormat
m_audioFileFormat
protected javax.sound.sampled.AudioInputStream
m_audioInputStream
protected java.lang.Object
m_dataSource
protected javax.sound.sampled.AudioInputStream
m_encodedaudioInputStream
protected javax.sound.sampled.FloatControl
m_gainControl
protected javax.sound.sampled.SourceDataLine
m_line
protected java.lang.String
m_mixerName
protected javax.sound.sampled.FloatControl
m_panControl
protected java.lang.Thread
m_thread
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.
-
Constructor Summary
Constructors Constructor Description BasicPlayer()
Constructs a Basic Player.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 java.util.Map
deepCopy(java.util.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.java.util.Collection
getListeners()
Return registered listeners.float
getMaximumGain()
Gets max Gain value.float
getMinimumGain()
Gets min Gain value.javax.sound.sampled.Mixer
getMixer(java.lang.String name)
java.lang.String
getMixerName()
java.util.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(java.io.File file)
Inits Audio ressources from file.protected void
initAudioInputStream(java.io.InputStream inputStream)
Inits Audio ressources from InputStream.protected void
initAudioInputStream(java.net.URL url)
Inits Audio ressources from URL.protected void
initLine()
Inits Audio ressources from AudioSystem.protected void
notifyEvent(int code, int position, double value, java.lang.Object description)
Notify listeners about a BasicPlayerEvent.void
open(java.io.File file)
Open file to play.void
open(java.io.InputStream inputStream)
Open inputstream to play.void
open(java.net.URL url)
Open URL to play.protected void
openLine()
Opens the line.void
pause()
Pause playback.protected void
pausePlayback()
Pauses the playback.
Player Status = PAUSED.void
play()
Start playback.void
removeBasicPlayerListener(BasicPlayerListener bpl)
Remove registered listener.protected void
reset()
void
resume()
Resume playback.protected void
resumePlayback()
Resumes the playback.
Player Status = PLAYING.void
run()
Main loop.long
seek(long bytes)
Skip bytes.void
setGain(double fGain)
Sets Gain value.void
setLineBufferSize(int size)
Set SourceDataLine buffer size.void
setMixerName(java.lang.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()
Stop playback.protected void
stopPlayback()
Stops the playback.
Player Status = STOPPED.
Thread should free Audio ressources.
-
-
-
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
-
PLAYING
public static final int PLAYING
- See Also:
- Constant Field Values
-
PAUSED
public static final int PAUSED
- See Also:
- Constant Field Values
-
STOPPED
public static final int STOPPED
- See Also:
- Constant Field Values
-
OPENED
public static final int OPENED
- See Also:
- Constant Field Values
-
SEEKING
public static final int SEEKING
- See Also:
- Constant Field Values
-
-
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
-
open
public void open(java.io.File file) throws BasicPlayerException
Open file to play.- Specified by:
open
in interfaceBasicController
- Throws:
BasicPlayerException
-
open
public void open(java.net.URL url) throws BasicPlayerException
Open URL to play.- Specified by:
open
in interfaceBasicController
- Throws:
BasicPlayerException
-
open
public void open(java.io.InputStream inputStream) throws BasicPlayerException
Open inputstream to play.- Specified by:
open
in interfaceBasicController
- Throws:
BasicPlayerException
-
initAudioInputStream
protected void initAudioInputStream() throws BasicPlayerException
Inits AudioInputStream and AudioFileFormat from the data source.- Throws:
BasicPlayerException
-
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.
-
startPlayback
protected void startPlayback() throws BasicPlayerException
Starts playback.- Throws:
BasicPlayerException
-
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 interfacejava.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:
-
seek
public long seek(long bytes) throws BasicPlayerException
Description copied from interface:BasicController
Skip bytes.- Specified by:
seek
in interfaceBasicController
- Returns:
- bytes skipped according to audio frames constraint.
- Throws:
BasicPlayerException
- See Also:
BasicController.seek(long)
-
play
public void play() throws BasicPlayerException
Description copied from interface:BasicController
Start playback.- Specified by:
play
in interfaceBasicController
- Throws:
BasicPlayerException
- See Also:
BasicController.play()
-
stop
public void stop() throws BasicPlayerException
Description copied from interface:BasicController
Stop playback.- Specified by:
stop
in interfaceBasicController
- Throws:
BasicPlayerException
- See Also:
BasicController.stop()
-
pause
public void pause() throws BasicPlayerException
Description copied from interface:BasicController
Pause playback.- Specified by:
pause
in interfaceBasicController
- Throws:
BasicPlayerException
- See Also:
BasicController.pause()
-
resume
public void resume() throws BasicPlayerException
Description copied from interface:BasicController
Resume playback.- Specified by:
resume
in interfaceBasicController
- Throws:
BasicPlayerException
- See Also:
BasicController.resume()
-
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 interfaceBasicController
- Parameters:
fPan
- value from -1.0 to +1.0- Throws:
BasicPlayerException
-
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 interfaceBasicController
- 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)
-
-