The Clips LoaderClipsLoader 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.
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
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.
Playing Clipsplay( ) 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. |