Holger's
Java API

com.antelmann.sound
Class SoundPlayer

java.lang.Object
  extended by com.antelmann.sound.SoundPlayer
Direct Known Subclasses:
MP3Player

public class SoundPlayer
extends Object

SoundPlayer provides a convenient and simple way for playing back sampled audio and midi files, URLs, streams and midi sequences.

Author:
Holger Antelmann

Nested Class Summary
static interface SoundPlayer.Listener
          This interface allows implementing classes to be notified when certain supported events by the SoundPlayer are triggered.
 
Field Summary
protected  AudioInputStream audioStream
           
protected  Clip clip
           
protected  Object currentSoundSource
           
protected  Sequence sequence
           
protected  Sequencer sequencer
           
 
Constructor Summary
SoundPlayer()
          Initializes the SoundPlayer without loading any specific sound.
SoundPlayer(File file)
           
SoundPlayer(InputStream stream)
          accepts both midi and sampled audio streams
SoundPlayer(Sequence sequence)
           
SoundPlayer(URL url)
           
 
Method Summary
 void addListener(SoundPlayer.Listener listener)
           
static void beep()
          provides the standard beep sound
 void close()
          closes the current sound.
protected  void finalize()
           
 Object getCurrentSoundSource()
          returned object may be of type InputStream, URL or File.
 long getMicrosecondLength()
          if no active sound object is present, -1 is returned; note that this doesn't return milliseconds but microseconds; also, for sampled audio data, the length may be not accurate.
 long getMicrosecondPosition()
          returns the current microsecond position of the active sound object; returns -1 if no active sound object present
 String getSongInfo()
          returns the info for the current sound as a String to be displayed
 String getSoundTypeDescription()
           
 boolean isPlaying()
           
 void load(File file)
          closes the existing data and then tries to load the given sound object
protected  void load(Object sound)
          handles File and URL objects
 void load(SampleSounds sound)
           
 void load(Sequence sq)
          closes the existing data and then tries to load the given sound object
 void load(URL url)
          closes the existing data and then tries to load the given sound object
protected  void notifyListeners()
           
 void play()
          if play() is called on a sound that has been played through, you may have to call stop() before play() will have the effect of replaying the sound
static void play(URL url)
           
static void playAndWait(URL url)
           
 void setMicrosecondPosition(long microseconds)
          sets the microsecond position of the active sound object; the method call has no effect if no active sound object is present
 void stop()
          use play() to resume playing after stop() was called
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

currentSoundSource

protected Object currentSoundSource

audioStream

protected AudioInputStream audioStream

sequence

protected Sequence sequence

sequencer

protected Sequencer sequencer

clip

protected Clip clip
Constructor Detail

SoundPlayer

public SoundPlayer()
            throws SoundException
Initializes the SoundPlayer without loading any specific sound. You can use any of the load(..) methods to use that sound.

Throws:
SoundException - if the midi sequencer is unavailable

SoundPlayer

public SoundPlayer(File file)
            throws SoundException
Throws:
SoundException

SoundPlayer

public SoundPlayer(URL url)
            throws SoundException
Throws:
SoundException

SoundPlayer

public SoundPlayer(InputStream stream)
            throws SoundException
accepts both midi and sampled audio streams

Throws:
SoundException

SoundPlayer

public SoundPlayer(Sequence sequence)
            throws SoundException
Throws:
SoundException
Method Detail

beep

public static void beep()
provides the standard beep sound


load

public void load(Sequence sq)
          throws SoundException
closes the existing data and then tries to load the given sound object

Throws:
SoundException

load

public void load(URL url)
          throws SoundException
closes the existing data and then tries to load the given sound object

Throws:
SoundException

load

public void load(File file)
          throws SoundException
closes the existing data and then tries to load the given sound object

Throws:
SoundException

load

public void load(SampleSounds sound)
          throws SoundException
Throws:
SoundException

load

protected void load(Object sound)
             throws SoundException
handles File and URL objects

Throws:
SoundException

getMicrosecondLength

public long getMicrosecondLength()
if no active sound object is present, -1 is returned; note that this doesn't return milliseconds but microseconds; also, for sampled audio data, the length may be not accurate.

See Also:
getMicrosecondPosition(), setMicrosecondPosition(long)

getMicrosecondPosition

public long getMicrosecondPosition()
returns the current microsecond position of the active sound object; returns -1 if no active sound object present

See Also:
setMicrosecondPosition(long), getMicrosecondLength()

setMicrosecondPosition

public void setMicrosecondPosition(long microseconds)
sets the microsecond position of the active sound object; the method call has no effect if no active sound object is present

See Also:
getMicrosecondPosition(), getMicrosecondLength()

getCurrentSoundSource

public Object getCurrentSoundSource()
returned object may be of type InputStream, URL or File. (whatever type was used with the constructor or with the load(..) method)


getSoundTypeDescription

public String getSoundTypeDescription()

getSongInfo

public String getSongInfo()
returns the info for the current sound as a String to be displayed


close

public void close()
closes the current sound. You can reuse the same SoundPlayer by loading a new sound with one of the load(..) methods


play

public void play()
if play() is called on a sound that has been played through, you may have to call stop() before play() will have the effect of replaying the sound

See Also:
stop()

isPlaying

public boolean isPlaying()

stop

public void stop()
use play() to resume playing after stop() was called

See Also:
play()

finalize

protected void finalize()
Overrides:
finalize in class Object

addListener

public void addListener(SoundPlayer.Listener listener)

notifyListeners

protected void notifyListeners()

play

public static void play(URL url)

playAndWait

public static void playAndWait(URL url)


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads