I've made some modifications to the <sound> task:
  - If you specify a directory instead of a file for <success> and/or
    <fail>, the task will randomly select a file from the directory
    to play.
  - I made the default for "loops" 0 instead of 1, since that seemed
    like the more reasonable behaviour (ie., play the file once, not
    twice, by default).
  - I removed the BuildException if the <success> or <fail> element is
    missing or if the file/dir specified is bogus, since I don't think
    the <sound> task being bungered should be a reason to have your
    build actually fail.
  - I corrected the spelling of [Ss]ucessful, both in the comments and
    in one of the method names.         ^
  - Assorted other comments clean-up.
As to the first one, I was going to try and use the FilterFilename (or is
it FilenameFilter?) thing, but I couldn't find very good documentation for
it, and it looked like something you needed to understand to really be
able to use, so I bailed on it. Unfortunately, that means the user is
responsible for making sure any specified directory contains only sound
files. If anyone's interested in adding that, though, please feel free --
but I realize the <sound> task isn't a real high priority, just a bit of
fun (and something I could learn some more Java on).

Diane

=====
([EMAIL PROTECTED])



__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/
--- AntSoundPlayer.java.orig    Wed Jan  3 06:18:45 2001
+++ AntSoundPlayer.java Tue Jan  9 21:15:42 2001
@@ -81,11 +81,11 @@
 public class AntSoundPlayer implements LineListener, BuildListener {
 
     private File fileSuccess = null;
-    private int loopsSuccess = 1;
+    private int loopsSuccess = 0;
     private Long durationSuccess = null;
 
     private File fileFail = null;
-    private int loopsFail = 1;
+    private int loopsFail = 0;
     private Long durationFail = null;
 
     public AntSoundPlayer() {
@@ -93,11 +93,11 @@
     }
 
     /**
-     * @param fileName the location of the audio file to be played when the 
build is succesful
-     * @param loops the number of times the file should be played when the 
build is succesful
-     * @param duration the number of milliseconds the file should be played 
when the build is succesful
+     * @param source the location of the audio file to be played when the 
build is successful
+     * @param loops the number of times the file should be played when the 
build is successful
+     * @param duration the number of milliseconds the file should be played 
when the build is successful
      */
-    public void addBuildSuccesfulSound(File file, int loops, Long duration) {
+    public void addBuildSuccessfulSound(File file, int loops, Long duration) {
         this.fileSuccess = file;
         this.loopsSuccess = loops;
         this.durationSuccess = duration;
@@ -116,7 +116,7 @@
     }
 
     /**
-     * Plays the file for duration milliseconds or loops loops.
+     * Plays the file for duration milliseconds or loops.
      */
     private void play(Project project, File file, int loops, Long duration) {
 
@@ -137,7 +137,8 @@
 
                if (audioInputStream != null) {
                        AudioFormat     format = audioInputStream.getFormat();
-                       DataLine.Info   info = new DataLine.Info(Clip.class, 
format, AudioSystem.NOT_SPECIFIED);
+                       DataLine.Info   info = new DataLine.Info(Clip.class, 
format,
+                                             AudioSystem.NOT_SPECIFIED);
                        try {
                                audioClip = (Clip) AudioSystem.getLine(info);
                                audioClip.addLineListener(this);
@@ -160,7 +161,7 @@
             audioClip.close();
                }
                else {
-                       project.log("SoundTask: can't get data from file " + 
file.getName());
+                       project.log("Can't get data from file " + 
file.getName());
                }
     }
 
@@ -183,7 +184,8 @@
     }
 
     /**
-     * This is implemented to listen for any line events and closes the clip 
if required.
+     * This is implemented to listen for any line events and closes the
+     * clip if required.
      */
     public void update(LineEvent event) {
         if (event.getType().equals(LineEvent.Type.STOP)) {
@@ -217,7 +219,7 @@
         if (event.getException() == null && fileSuccess != null) {
             // build successfull!
             play(event.getProject(), fileSuccess, loopsSuccess, 
durationSuccess);
-        } else if (fileFail != null) {
+        } else if ( event.getException() != null && fileFail != null) {
             play(event.getProject(), fileFail, loopsFail, durationFail);
         }
     }
--- SoundTask.java.orig Wed Jan  3 06:18:45 2001
+++ SoundTask.java      Tue Jan  9 21:15:50 2001
@@ -62,16 +62,18 @@
 /**
  * This is an example of an AntTask that makes of use of the AntSoundPlayer.
  *
- * There are four attributes to be set:
+ * There are three attributes to be set:
  *
  * <code>source</code>: the location of the audio file to be played
  * <code>duration</code>: play the sound file continuously until "duration" 
milliseconds has expired
  * <code>loops</code>: the number of times the sound file should be played 
until stopped
  *
- * I have only tested this with .WAV and .AIFF sound file formats. Both seem 
to work fine.
+ * I have only tested this with .WAV and .AIFF sound file formats. Both seem
+ * to work fine.
  *
  * plans for the future:
- * - use the midi api to define sounds (or drum beat etc) in xml and have Ant 
play them back
+ * - use the midi api to define sounds (or drum beat etc) in xml and have
+ *   Ant play them back
  *
  * @author Nick Pellow
  * @version $Revision: 1.2 $, $Date: 2001/01/03 14:18:45 $
@@ -79,7 +81,7 @@
 
 public class SoundTask extends Task {
 
-    private BuildAlert  success = null;
+    private BuildAlert success = null;
     private BuildAlert fail = null;
 
     public BuildAlert createSuccess() {
@@ -98,28 +100,35 @@
     public void init(){
     }
 
-    public void execute() throws BuildException {
-        if ( success == null && fail == null) {
-            throw new BuildException("No nested elements provided.");
-        }
+    public void execute() {
 
         AntSoundPlayer soundPlayer = new AntSoundPlayer();
-        if (success != null) {
-            soundPlayer.addBuildSuccesfulSound(success.getSource(), 
success.getLoops(), success.getDuration());
+
+        if ( success == null ) {
+            log("No nested success element found.", Project.MSG_WARN);
+        } else {
+            soundPlayer.addBuildSuccessfulSound(success.getSource(),
+              success.getLoops(), success.getDuration());
         }
-        
-        if (fail != null) {
-            soundPlayer.addBuildFailedSound(fail.getSource(), fail.getLoops(), 
fail.getDuration());
+
+        if (fail == null) {
+            log("No nested failure element found.", Project.MSG_WARN);
+        } else {
+            soundPlayer.addBuildFailedSound(fail.getSource(),
+              fail.getLoops(), fail.getDuration());
         }
+
         getProject().addBuildListener(soundPlayer);
+
     }
 
     /**
-     * A static class to be extended by any BuildAlert's that require the 
output of sound.
+     * A class to be extended by any BuildAlert's that require the output
+     * of sound.
      */
-    public static class BuildAlert {
-        private File file = null;
-        private int loops = 1;
+    public class BuildAlert {
+        private File source = null;
+        private int loops = 0;
         private Long duration = null;
 
         /**
@@ -132,15 +141,14 @@
         /**
          * Sets the location of the file to get the audio.
          *
-         * @param fileName the location of the audio file
+         * @param source the name a sound-file directory or of the audio file
          */
-        public void setSource(File file) {
-            this.file = file;
+        public void setSource(File source) {
+            this.source = source;
         }
 
         /**
-         * This attribute sets the number of times the source file should
-         * be played.
+         * Sets the number of times the source file should be played.
          *
          * @param loops the number of loops to play the source file
          */
@@ -149,27 +157,43 @@
         }
 
         /**
-         * Gets the duration in milliseconds the file should be played.
-         */
-        public Long getDuration() {
-            return this.duration;
-        }
-
-        /**
          * Gets the location of the file to get the audio.
          */
         public File getSource() {
-            return this.file;
+            File nofile = null ;
+            // Check if source is a directory
+            if( source.exists() ) {
+                if( source.isDirectory() ) {
+                    // get the list of files in the dir
+                    File[] files = source.listFiles() ; 
+                    int numfiles = files.length ;
+                    // get a random number between 0 and the number of files
+                    Random rn = new Random() ;
+                    int i = rn.nextInt(numfiles) ;
+                    // set the source to the file at that location
+                    this.source = files[i] ;
+                }
+            } else {
+                log(source + ": invalid path.", Project.MSG_WARN) ;
+                this.source = nofile ;
+            }
+            return this.source ;
         }
 
         /**
-         * This attribute sets the number of times the source file should
-         * be played.
+         * Sets the number of times the source file should be played.
          *
          * @return the number of loops to play the source file
          */
         public int getLoops() {
             return this.loops;
+        }
+
+        /**
+         * Gets the duration in milliseconds the file should be played.
+         */
+        public Long getDuration() {
+            return this.duration;
         }
     }
 }

Reply via email to