e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.optional.sound.SoundTask   ©

 OK to copy?
001 /*
002  * Copyright  2000-2002,2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 
018 package org.apache.tools.ant.taskdefs.optional.sound;
019 
020 import java.io.File;
021 import java.util.Random;
022 import java.util.Vector;
023 import org.apache.tools.ant.BuildException;
024 import org.apache.tools.ant.Project;
025 import org.apache.tools.ant.Task;
026 
027 /**
028  * Plays a sound file at the end of the build, according to whether the build failed or succeeded.
029  *
030  * There are three attributes to be set:
031  *
032  * <code>source</code>: the location of the audio file to be played
033  * <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
034  * <code>loops</code>: the number of times the sound file should be played until stopped
035  *
036  * I have only tested this with .WAV and .AIFF sound file formats. Both seem
037  * to work fine.
038  *
039  * plans for the future:
040  * - use the midi api to define sounds (or drum beat etc) in xml and have
041  *   Ant play them back
042  *
043  * @version $Revision: 1.12.2.4 $, $Date: 2004/03/09 17:01:53 $
044  */
045 
046 public class SoundTask extends Task {
047 
048     private BuildAlert success = null;
049     private BuildAlert fail = null;
050 
051     /**
052      * add a sound when the build succeeds
053      */
054     public BuildAlert createSuccess() {
055         success = new BuildAlert();
056         return success;
057     }
058 
059     /**
060      * add a sound when the build fails
061      */
062     public BuildAlert createFail() {
063         fail = new BuildAlert();
064         return fail;
065      }
066 
067     public SoundTask() {
068     }
069 
070     public void init() {
071     }
072 
073     public void execute() {
074 
075         AntSoundPlayer soundPlayer = new AntSoundPlayer();
076 
077         if (success == null) {
078             log("No nested success element found.", Project.MSG_WARN);
079         } else {
080             soundPlayer.addBuildSuccessfulSound(success.getSource(),
081               success.getLoops(), success.getDuration());
082         }
083 
084         if (fail == null) {
085             log("No nested failure element found.", Project.MSG_WARN);
086         } else {
087             soundPlayer.addBuildFailedSound(fail.getSource(),
088               fail.getLoops(), fail.getDuration());
089         }
090 
091         getProject().addBuildListener(soundPlayer);
092 
093     }
094 
095     /**
096      * A class to be extended by any BuildAlert's that require the output
097      * of sound.
098      */
099     public class BuildAlert {
100         private File source = null;
101         private int loops = 0;
102         private Long duration = null;
103 
104         /**
105          * Sets the duration in milliseconds the file should be played; optional.
106          */
107         public void setDuration(Long duration) {
108             this.duration = duration;
109         }
110 
111         /**
112          * Sets the location of the file to get the audio; required.
113          *
114          * @param source the name of a sound-file directory or of the audio file
115          */
116         public void setSource(File source) {
117             this.source = source;
118         }
119 
120         /**
121          * Sets the number of times the source file should be played; optional.
122          *
123          * @param loops the number of loops to play the source file
124          */
125         public void setLoops(int loops) {
126             this.loops = loops;
127         }
128 
129         /**
130          * Gets the location of the file to get the audio.
131          */
132         public File getSource() {
133             File nofile = null;
134             // Check if source is a directory
135             if (source.exists()) {
136                 if (source.isDirectory()) {
137                     // get the list of files in the dir
Rate138                     String[] entries = source.list();
139                     Vector files = new Vector();
140                     for (int i = 0; i < entries.length; i++) {
141                         File f = new File(source, entries[i]);
142                         if (f.isFile()) {
143                             files.addElement(f);
144                         }
145                     }
146                     if (files.size() < 1) {
147                         throw new BuildException("No files found in directory " + source);
148                     }
149                     int numfiles = files.size();
150                     // get a random number between 0 and the number of files
151                     Random rn = new Random();
152                     int x = rn.nextInt(numfiles);
153                     // set the source to the file at that location
154                     this.source = (File) files.elementAt(x);
155                 }
156             } else {
157                 log(source + ": invalid path.", Project.MSG_WARN);
158                 this.source = nofile;
159             }
160             return this.source;
161         }
162 
163         /**
164          * Sets the number of times the source file should be played.
165          *
166          * @return the number of loops to play the source file
167          */
168         public int getLoops() {
169             return this.loops;
170         }
171 
172         /**
173          * Gets the duration in milliseconds the file should be played.
174          */
175         public Long getDuration() {
176             return this.duration;
177         }
178     }
179 }


            
All Examples in File:
Example
Line
Rating (found
useful by...)
138 0% of 0