group1.audio
Class MusicPlayer

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

public class MusicPlayer
extends java.lang.Thread

A class to play audio files. Spawns a separate thread, and can play looped music or sound effects. Additionally, several audio files can be played simultaneously using multiple objects.

Since this is a thread, you should create a MusicPlayer object, then call "start()" for that object. To safely stop the thread, call "stopAudio()".

To pause the thread, use "pause()". To unpause it, you need to use "syncronized", then "unpause()" and "notify()". (See unpause() javadoc for details.)

This only seems to work with certain formats of .wav files. It might work with a few others. ...Or, maybe not. Also, note that looped music must be set up first! That is, the end of the clip needs to work seamlessly into the beginning.

(Luckily, I've figured out how to convert nearly all audio files to a format that works. Plus, I'm able to edit a .wav file, so I can make it loop properly so long as there's no intro part. If you really want an intro, though, you could use two separate files. Or, I could write methods special for it.)


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
MusicPlayer(java.lang.String file, boolean looping)
          Constructor.
 
Method Summary
static void main(java.lang.String[] args)
          Main used for testing.
 void pause()
          Pause the thread.
 void run()
          Play the audio, using a loop that reads the audio byte-by-byte.
 void stopAudio()
          Stop the audio.
 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

MusicPlayer

public MusicPlayer(java.lang.String file,
                   boolean looping)
Constructor. Also, sets up the data stream, but doesn't start the audio just yet.

Parameters:
file - The name of the file for the music.
looping - If the audio clip should be looped.
Method Detail

main

public static void main(java.lang.String[] args)
Main used for testing. (Yes, it works.)


run

public void run()
Play the audio, using a loop that reads the audio byte-by-byte. A method called by the system to run the thread. Automatically called after calling start().

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

stopAudio

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


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 (thisAudioObject) { thisAudioObject.unpause(); thisAudioObject.notify(); }