|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Thread group1.view.audio.MusicPlayer
public class MusicPlayer
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 |
---|
public MusicPlayer(java.lang.String file, boolean looping)
file
- The name of the file for the music.looping
- If the audio clip should be looped.Method Detail |
---|
public static void main(java.lang.String[] args)
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public void stopAudio()
public void pause()
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.
public void unpause()
Note that this only part of the unpausing procedure! To correctly unpause the audio, use something like: synchronized (thisAudioObject) { thisAudioObject.unpause(); thisAudioObject.notify(); }
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |