com.lightdev.lib.audio
Class Player


java.lang.Object

  extended by java.lang.Thread

      extended by com.lightdev.lib.audio.Player

All Implemented Interfaces:
Runnable

public class Player
extends Thread

Audio player for MP3 files

Using this class requires the MP3 decoder plug-in at http://www.tritonus.org/plugins.html or another component that implements the Java Sound API.

Class Player only plays audio when no PlayerListener is registered or when none of the registered listeners signals the player that it consumes decoded bytes. When a listener signals the player that it consumes decoded bytes, the player does not play audio, it only passes decoded bytes to the listener. The listener is responsible to pass decoded bytes back to the player with a call to method Player.write() to actually play audio.

Version:
Version 1, September 5, 2004
Author:
Ulrich Hilger, Light Development, http://www.lightdev.com, info@lightdev.com, published under the terms and conditions of the GNU General Public License, for details see file 'license.txt' in the distribution package of this software

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static int INITIALIZING
          constant value for status initializing (i.e. not ready to play yet)
static int NONE
          constant value to indicate that no status has been set so far
static int PAUSED
          constant value for status paused (i.e. thread still active, can resume play)
static int PLAYING
          constant value for status playing
static int READY
          constant value for status ready to play
static int SKIPPING
          constant value to indicate that the player currently repositions by skipping a given amount of bytes
static int STOPPED_EOM
          constant value for status stopped due to end of media (thread will terminate irrevocably immediately after)
static int STOPPED_PLAY
          constant value for status stopped during play (thread will terminate irrevocably immediately after)
static int TERMINATED
          constant value for a terminated thread
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Player()
          constructor
 
Method Summary
 void addPlayerListener(PlayerListener l)
          add a PlayerListener to this Player thread
 String getAlbum()
          get the album of the currently playing song
 String getArtist()
          get the artist of the currently playing song
 float getAudioFrameRate()
          get the number of frames per second for this audio input stream
 float getBalance()
          get the balance
 int getChannels()
          get the number of channels for this audio input stream
 long getDuration()
          get the duration of the currently playing song
 int getFrameCount()
          get the frame count
 double getFrameRate()
          get the frame rate
 int getFrameSize()
          get the size of a frame in bytes
 float getSampleRate()
          get the number of samples per second
 int getSampleSizeInBits()
          get the size of one sample in bits
 long getSizeBytes()
          get the length of the file in bytes
 SourceDataLine getSourceDataLine()
          get the SourceDataLine of this Player
 String getTitle()
          get the title of the currently playing song
 float getVolume()
          get the volume
 boolean isBigEndian()
          dtermine wether or not this audio input stream is big endian
 boolean isPaused()
          determine whether or not this Player is paused
 void pausePlay()
          pause playing Keeps this instance of Player thread running (sleeping until it is resumed or stopped, that is)
 void removePlayerListener(PlayerListener l)
          remove a PlayerListener from this Player thread
 void resumePlay()
          resume playing a paused play
 void run()
          run this thread, i.e. play an audio file Don't use this method directly, use startPlay instead
 void setBalance(float balance)
          set the balance of this player to a value of -100 (left) and 100 (right); 0 means equal distribution between left and right
 void setStatus(int newStatus)
          set the status of this Player object to a given new status
 void setVolume(float vol, float correction)
          set the volume of this player to a value between 0 (sound off) and 100 percent.
 void startPlay(File file)
          start to play a given audio file
 void startPlay(File file, long startPos)
          start to play a given audio file at a certain position
 void stopPlay()
          stop playing Will terminate this instance of Player thread without the possibility to restart it.
 void write(byte[] abData, int nBytes)
          write some audio bytes to the source data line of this player
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NONE


public static final int NONE
constant value to indicate that no status has been set so far

See Also:
Constant Field Values

INITIALIZING


public static final int INITIALIZING
constant value for status initializing (i.e. not ready to play yet)

See Also:
Constant Field Values

SKIPPING


public static final int SKIPPING
constant value to indicate that the player currently repositions by skipping a given amount of bytes

See Also:
Constant Field Values

READY


public static final int READY
constant value for status ready to play

See Also:
Constant Field Values

PLAYING


public static final int PLAYING
constant value for status playing

See Also:
Constant Field Values

PAUSED


public static final int PAUSED
constant value for status paused (i.e. thread still active, can resume play)

See Also:
Constant Field Values

STOPPED_PLAY


public static final int STOPPED_PLAY
constant value for status stopped during play (thread will terminate irrevocably immediately after)

See Also:
Constant Field Values

STOPPED_EOM


public static final int STOPPED_EOM
constant value for status stopped due to end of media (thread will terminate irrevocably immediately after)

See Also:
Constant Field Values

TERMINATED


public static final int TERMINATED
constant value for a terminated thread

See Also:
Constant Field Values
Constructor Detail

Player


public Player()
constructor

Method Detail

startPlay


public void startPlay(File file)
start to play a given audio file

Parameters:
file - File the audio file to play

getSourceDataLine


public SourceDataLine getSourceDataLine()
get the SourceDataLine of this Player

Returns:
the SourceDataLine, if any

startPlay


public void startPlay(File file,

                      long startPos)
start to play a given audio file at a certain position

Parameters:
file - File the audio file to start to play
startPos - long the byte position to start playing at

stopPlay


public void stopPlay()
stop playing

Will terminate this instance of Player thread without the possibility to restart it.


pausePlay


public void pausePlay()
pause playing

Keeps this instance of Player thread running (sleeping until it is resumed or stopped, that is)

See Also:
resumePlay

resumePlay


public void resumePlay()
resume playing a paused play

See Also:
pausePlay

setStatus


public void setStatus(int newStatus)
set the status of this Player object to a given new status

Parameters:
newStatus - int the new status to set this player to

setVolume


public void setVolume(float vol,

                      float correction)
set the volume of this player to a value between 0 (sound off) and 100 percent.

Parameters:
vol - float
correction - float value to add to skip non hearable region

getVolume


public float getVolume()
get the volume

Returns:
float the volume between 0 (sound off) and 100 percent

setBalance


public void setBalance(float balance)
set the balance of this player to a value of -100 (left) and 100 (right); 0 means equal distribution between left and right

Parameters:
balance - float

getBalance


public float getBalance()
get the balance

Returns:
float the balance, a value between -100 (left) and 100 (right)

getDuration


public long getDuration()
get the duration of the currently playing song

Returns:
long duration in milliseconds

getTitle


public String getTitle()
get the title of the currently playing song

Returns:
String the song title

getArtist


public String getArtist()
get the artist of the currently playing song

Returns:
String the artist of the song

getAlbum


public String getAlbum()
get the album of the currently playing song

Returns:
String the album of the song

getFrameCount


public int getFrameCount()
get the frame count

Returns:
long the number of frames in the file

getFrameRate


public double getFrameRate()
get the frame rate

Returns:
int the number of frames per second

getSizeBytes


public long getSizeBytes()
get the length of the file in bytes

Returns:
long

getSampleSizeInBits


public int getSampleSizeInBits()
get the size of one sample in bits

Returns:
int the sample size in bits

getFrameSize


public int getFrameSize()
get the size of a frame in bytes

Returns:
int the frame size in bytes

getAudioFrameRate


public float getAudioFrameRate()
get the number of frames per second for this audio input stream

Returns:
float te number of frames per second

getSampleRate


public float getSampleRate()
get the number of samples per second

Returns:
float the sample rate

isBigEndian


public boolean isBigEndian()
dtermine wether or not this audio input stream is big endian

Returns:
boolean true if yes

getChannels


public int getChannels()
get the number of channels for this audio input stream

Returns:
int the number of channels

write


public void write(byte[] abData,

                  int nBytes)
write some audio bytes to the source data line of this player

Parameters:
abData - byte[] byte buffer having bytes to write
nBytes - int the number of bytes from abData to write out

isPaused


public boolean isPaused()
determine whether or not this Player is paused

Returns:
boolean true, if this thread is paused, false if not

run


public void run()
run this thread, i.e. play an audio file

Don't use this method directly, use startPlay instead

Specified by:
run in interface Runnable
Overrides:
run in class Thread
See Also:
Player.startPlay()

addPlayerListener


public void addPlayerListener(PlayerListener l)
add a PlayerListener to this Player thread

Parameters:
l - PlayerListener the listener to add

removePlayerListener


public void removePlayerListener(PlayerListener l)
remove a PlayerListener from this Player thread

Parameters:
l - PlayerListener the listener to remove

Copyright (c) 2004, 2005 Ulrich Hilger