Storing Clip Information


The Clips Loader

ClipsLoader stores a collection of ClipInfo objects in a HashMap, keyed by their names. The name and filename for a clip are obtained from a sounds information file, which is loaded when ClipsLoader is created. The information file is assumed to be in the subdirectory Sounds/.

ClipsLoader allows a specified clip to be played, paused, resumed, looped, and stopped. A SoundsWatcher can be attached to a clip. All this functionality is handled in the ClipInfo object for the clip.

It's possible for many clips to play simultaneously, since each ClipInfo object is responsible for playing its own clip.


The first ClipsLoader constructor loads a sounds information file, and the second initializes the HashMap of clips:

     // globals     private HashMap clipsMap;       /* The key is the clip 'name', the object (value)          is a ClipInfo object /     public ClipsLoader(String soundsFnm)     { this( );       loadSoundsFile(soundsFnm);     }     public ClipsLoader( )     {  clipsMap = new HashMap( );  }

loadSoundsFile( ) parses the information file, assuming each line contains a name and filename. For example, clipsInfo.txt used by LoadersTests is:

     // sounds     cat cat.wav     chicken chicken.wav     dog dog.wav     sheep sheep.wav

The name can be any string. The file may contain blank lines and comment lines beginning with //.


After a line's name and filename have been extracted, load( ) is called:

     public void load(String name, String fnm)     // create a ClipInfo object for name and store it     {       if (clipsMap.containsKey(name))         System.out.println( "Error: " + name + "already stored");       else {                 clipsMap.put(name, new ClipInfo(name, fnm) );         System.out.println("-- " + name + "/" + fnm);       }     }

A ClipInfo object is created, and added to the HashMap.

load( ) is public so a user can directly add clips to the loader.


Playing Clips

play( ) illustrates the coding style used by the other public methods in ClipsLoader. In each method (play( ), close( ), stop( ), pause( ), resume( ), and setWatcher( )), the name of the clip is provided, along with if it should be looped. The ClipInfo object is retrieved using that name, errors are handled, and then the requested operation is delegated to the object:

     public void play(String name, boolean toLoop)     // play (perhaps loop) the specified clip     {  ClipInfo ci = (ClipInfo) clipsMap.get(name);        if (ci == null)          System.out.println( "Error: " + name + "not stored");        else         ci.play(toLoop);   // delegate operation to ClipInfo obj     }

Audio manipulation is delegated to the ClipInfo object associated with the specified clip name.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net