group1.view.audio
Class AudioChannel

java.lang.Object
  extended by java.lang.Thread
      extended by group1.view.audio.AudioChannel
All Implemented Interfaces:
java.lang.Runnable

public class AudioChannel
extends java.lang.Thread

A thread used to play audio. This thread stays around even after the audio finishes, and can be used over and over. (It is recommended that the view create several of these channels, perhaps 4-8, and use them as they are available.)

Supports pausing, unpausing, looping, and stopping audio.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
AudioChannel(javax.sound.sampled.AudioFormat format)
          Constructor.
 
Method Summary
 boolean hasAudioData()
          Returns whether or not this thread is busy playing audio (or cleaning it up).
 void pause()
          Pause the thread.
 void playAudio(AudioCache cache, boolean looping)
          Play the requested audio from a cache.
 void run()
          A method called by the system to run the thread.
 void stopAudio()
          Stop the current audio.
 void stopThread()
          Stop the thread.
 void unpause()
          Unpause the audio.
 
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
 

Constructor Detail

AudioChannel

public AudioChannel(javax.sound.sampled.AudioFormat format)
Constructor. Starts the thread.

Parameters:
format - The audio format to use.
Method Detail

run

public void run()
A method called by the system to run the thread. (Plays audio whenever it's given audio data.) Automatically called after calling start() in the constructor.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

playAudio

public void playAudio(AudioCache cache,
                      boolean looping)
               throws BusyAudioChannelException
Play the requested audio from a cache.

Parameters:
cache - The AudioCache where the audio data to be played is stored.
looping - Whether or not this audio should be looped.
Throws:
BusyAudioChannelException - If the channel is already busy playing (or still cleaning up) audio.

stopThread

public void stopThread()
Stop the thread. Any audio will be stopped immediately, and the thread will exit.


stopAudio

public void stopAudio()
Stop the current audio. Any audio will be stopped immediately, and the thread will be free to play new audio files after the clean-up.


pause

public void pause()
Pause the thread. The audio will be temporarily paused.

Note that this pauses the THREAD, not just the audio! To unpause the thread, the thread that hold's the object for this thread needs to call this thread's notify method, and should do use syncronized. See unpause for an example.


unpause

public void unpause()
Unpause the audio. The audio will be set up to resume, when the thread is notified to start again.

Note that this only part of the unpausing procedure! To correctly unpause the audio, use something like: synchronized (thisAudioChannel) { thisAudioChannel.unpause(); thisAudioChannel.notify(); }


hasAudioData

public boolean hasAudioData()
Returns whether or not this thread is busy playing audio (or cleaning it up).

Returns:
If this has audio data.