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 intencodedLengthstatic intEXTERNAL_BUFFER_SIZEprotected javax.sound.sampled.AudioFileFormatm_audioFileFormatprotected javax.sound.sampled.AudioInputStreamm_audioInputStreamprotected java.lang.Objectm_dataSourceprotected javax.sound.sampled.AudioInputStreamm_encodedaudioInputStreamprotected javax.sound.sampled.FloatControlm_gainControlprotected javax.sound.sampled.SourceDataLinem_lineprotected java.lang.Stringm_mixerNameprotected javax.sound.sampled.FloatControlm_panControlprotected java.lang.Threadm_threadstatic intOPENEDstatic intPAUSEDstatic intPLAYINGstatic intSEEKINGstatic intSKIP_INACCURACY_SIZEstatic intSTOPPEDstatic intUNKNOWNThese 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 voidaddBasicPlayerListener(BasicPlayerListener bpl)Add listener to be notified.protected voidcloseStream()protected voidcreateLine()Inits a DateLine.
We check if the line supports Gain and Pan controls.protected java.util.MapdeepCopy(java.util.Map src)Deep copy of a Map.protected intgetEncodedStreamPosition()floatgetGainValue()Returns Gain value.intgetLineBufferSize()Return SourceDataLine buffer size.intgetLineCurrentBufferSize()Return SourceDataLine current buffer size.java.util.CollectiongetListeners()Return registered listeners.floatgetMaximumGain()Gets max Gain value.floatgetMinimumGain()Gets min Gain value.javax.sound.sampled.MixergetMixer(java.lang.String name)java.lang.StringgetMixerName()java.util.ListgetMixers()floatgetPan()Returns Pan value.floatgetPrecision()Returns Pan precision.longgetSleepTime()Return thread sleep time in milliseconds.intgetStatus()Returns BasicPlayer status.booleanhasGainControl()Returns true if Gain control is supported.booleanhasPanControl()Returns true if Pan control is supported.protected voidinitAudioInputStream()Inits AudioInputStream and AudioFileFormat from the data source.protected voidinitAudioInputStream(java.io.File file)Inits Audio ressources from file.protected voidinitAudioInputStream(java.io.InputStream inputStream)Inits Audio ressources from InputStream.protected voidinitAudioInputStream(java.net.URL url)Inits Audio ressources from URL.protected voidinitLine()Inits Audio ressources from AudioSystem.protected voidnotifyEvent(int code, int position, double value, java.lang.Object description)Notify listeners about a BasicPlayerEvent.voidopen(java.io.File file)Open file to play.voidopen(java.io.InputStream inputStream)Open inputstream to play.voidopen(java.net.URL url)Open URL to play.protected voidopenLine()Opens the line.voidpause()Pause playback.protected voidpausePlayback()Pauses the playback.
Player Status = PAUSED.voidplay()Start playback.voidremoveBasicPlayerListener(BasicPlayerListener bpl)Remove registered listener.protected voidreset()voidresume()Resume playback.protected voidresumePlayback()Resumes the playback.
Player Status = PLAYING.voidrun()Main loop.longseek(long bytes)Skip bytes.voidsetGain(double fGain)Sets Gain value.voidsetLineBufferSize(int size)Set SourceDataLine buffer size.voidsetMixerName(java.lang.String name)voidsetPan(double fPan)Sets Pan value.voidsetSleepTime(long time)Set thread sleep time.protected longskipBytes(long bytes)Skip bytes in the File inputstream.protected voidstartPlayback()Starts playback.voidstop()Stop playback.protected voidstopPlayback()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 BasicPlayerExceptionOpen file to play.- Specified by:
openin interfaceBasicController- Throws:
BasicPlayerException
-
open
public void open(java.net.URL url) throws BasicPlayerExceptionOpen URL to play.- Specified by:
openin interfaceBasicController- Throws:
BasicPlayerException
-
open
public void open(java.io.InputStream inputStream) throws BasicPlayerExceptionOpen inputstream to play.- Specified by:
openin interfaceBasicController- Throws:
BasicPlayerException
-
initAudioInputStream
protected void initAudioInputStream() throws BasicPlayerExceptionInits AudioInputStream and AudioFileFormat from the data source.- Throws:
BasicPlayerException
-
initAudioInputStream
protected void initAudioInputStream(java.io.File file) throws javax.sound.sampled.UnsupportedAudioFileException, java.io.IOExceptionInits Audio ressources from file.- Throws:
javax.sound.sampled.UnsupportedAudioFileExceptionjava.io.IOException
-
initAudioInputStream
protected void initAudioInputStream(java.net.URL url) throws javax.sound.sampled.UnsupportedAudioFileException, java.io.IOExceptionInits Audio ressources from URL.- Throws:
javax.sound.sampled.UnsupportedAudioFileExceptionjava.io.IOException
-
initAudioInputStream
protected void initAudioInputStream(java.io.InputStream inputStream) throws javax.sound.sampled.UnsupportedAudioFileException, java.io.IOExceptionInits Audio ressources from InputStream.- Throws:
javax.sound.sampled.UnsupportedAudioFileExceptionjava.io.IOException
-
initLine
protected void initLine() throws javax.sound.sampled.LineUnavailableExceptionInits Audio ressources from AudioSystem.- Throws:
javax.sound.sampled.LineUnavailableException
-
createLine
protected void createLine() throws javax.sound.sampled.LineUnavailableExceptionInits 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.LineUnavailableExceptionOpens 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 BasicPlayerExceptionStarts 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:
runin interfacejava.lang.Runnable
-
skipBytes
protected long skipBytes(long bytes) throws BasicPlayerExceptionSkip 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 BasicPlayerExceptionDescription copied from interface:BasicControllerSkip bytes.- Specified by:
seekin interfaceBasicController- Returns:
- bytes skipped according to audio frames constraint.
- Throws:
BasicPlayerException- See Also:
BasicController.seek(long)
-
play
public void play() throws BasicPlayerExceptionDescription copied from interface:BasicControllerStart playback.- Specified by:
playin interfaceBasicController- Throws:
BasicPlayerException- See Also:
BasicController.play()
-
stop
public void stop() throws BasicPlayerExceptionDescription copied from interface:BasicControllerStop playback.- Specified by:
stopin interfaceBasicController- Throws:
BasicPlayerException- See Also:
BasicController.stop()
-
pause
public void pause() throws BasicPlayerExceptionDescription copied from interface:BasicControllerPause playback.- Specified by:
pausein interfaceBasicController- Throws:
BasicPlayerException- See Also:
BasicController.pause()
-
resume
public void resume() throws BasicPlayerExceptionDescription copied from interface:BasicControllerResume playback.- Specified by:
resumein interfaceBasicController- Throws:
BasicPlayerException- See Also:
BasicController.resume()
-
setPan
public void setPan(double fPan) throws BasicPlayerExceptionSets Pan value. Line should be opened before calling this method. Linear scale : -1.0 <--> +1.0- Specified by:
setPanin interfaceBasicController- Parameters:
fPan- value from -1.0 to +1.0- Throws:
BasicPlayerException
-
setGain
public void setGain(double fGain) throws BasicPlayerExceptionSets 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:
setGainin 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)
-
-