mhframework.media
Class MHSoundManager

java.lang.Object
  extended by mhframework.media.MHSoundManager
All Implemented Interfaces:
java.util.EventListener, javax.sound.sampled.LineListener

public class MHSoundManager
extends java.lang.Object
implements javax.sound.sampled.LineListener

Manages all sound data for a game application made with the MHFramework package. I recommend that maintaining the sound manager for a game application should be the responsibility of the application's data model.

This class borrows heavily from the SoundManager class presented in the the book Java 1.4 Game Programming by Andrew Mulholland and Glenn Murphy (Wordware Publishing).


Field Summary
static int AUTO_ASSIGN_CHANNEL
          Constant for telling the MHSoundManager object to automatically assign an available channel number for playing a sound.
 
Constructor Summary
MHSoundManager()
          Constructor.
 
Method Summary
 int addSound(java.lang.String filepath)
          Adds a sound file to the sound manager.
 boolean isChannelPlaying(int channelId)
          States whether the requested channel is currently playing a sound.
 boolean isSoundPlaying(int soundId)
          States whether the sound specified by soundId is currently playing.
 void play(int soundId, boolean loop, int channelId)
          Plays a sound.
 void stop(int soundId)
          Stops the sound specified by soundId if it is currently playing.
 void stopAllChannels()
          Stops all active channels.
 void stopChannel(int channelId)
          Stops the sound playing in the channel specified by channelId.
 void update(javax.sound.sampled.LineEvent e)
          Event handler for line events.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTO_ASSIGN_CHANNEL

public static final int AUTO_ASSIGN_CHANNEL
Constant for telling the MHSoundManager object to automatically assign an available channel number for playing a sound.

See Also:
Constant Field Values
Constructor Detail

MHSoundManager

public MHSoundManager()
Constructor. Instantiates internal data structures and initializes the channel IDs.

Method Detail

addSound

public int addSound(java.lang.String filepath)
Adds a sound file to the sound manager.

Parameters:
filepath - The path and file name of the sound file to be added.
Returns:
If successful, the position in the sound data vector where the new sound byte data was stored; otherwise -1.

play

public void play(int soundId,
                 boolean loop,
                 int channelId)
Plays a sound. Loads sound byte data into the channel specified by the input channel ID. If the channelId parameter is the constant MHSoundManager.AUTO_ASSIGN_CHANNEL, then an available channel is automatically assigned. The sound data is then looped if loop is true, or just played once if loop is false.

Note: This method was made "synchronized" due to problems with calling it from event handlers. The same channel was being allocated to multiple sound instances, which caused the program to hang.

Parameters:
soundId - ID of which sound to play.
loop - True if sound should be looped.
channelId - ID of the channel on which to play the sound.

stop

public void stop(int soundId)
Stops the sound specified by soundId if it is currently playing.

Parameters:
soundId - The ID of the sound to be stopped.

stopChannel

public void stopChannel(int channelId)
Stops the sound playing in the channel specified by channelId.

Parameters:
channelId - The channel containing the sound to be stopped.

isChannelPlaying

public boolean isChannelPlaying(int channelId)
States whether the requested channel is currently playing a sound.

Checks two conditions to determine this:

  1. Does the channel contain sound byte data?
  2. Is the channel open?

If both of the above conditions are true, the channel is playing a sound.

Parameters:
channelId - The ID of the channel to be checked for sound data.
Returns:
True if channel is playing a sound; false otherwise.

isSoundPlaying

public boolean isSoundPlaying(int soundId)
States whether the sound specified by soundId is currently playing.

Parameters:
soundId - The ID of the sound in question.
Returns:
True if sound is playing; false otherwise.

update

public void update(javax.sound.sampled.LineEvent e)
Event handler for line events. If the type of event is LineEvent.Type.STOP, this method finds the channel associated with the line and resets it, and then closes the line.

Specified by:
update in interface javax.sound.sampled.LineListener
Parameters:
e - The line event being handled.

stopAllChannels

public void stopAllChannels()
Stops all active channels.