KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sound > SoundTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.sound;
20
21 import java.io.File JavaDoc;
22 import java.util.Random JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.Task;
27
28 /**
29  * Plays a sound file at the end of the build, according to whether the build failed or succeeded.
30  *
31  * There are three attributes to be set:
32  *
33  * <code>source</code>: the location of the audio file to be played
34  * <code>duration</code>: play the sound file continuously until "duration" milliseconds has expired
35  * <code>loops</code>: the number of times the sound file should be played until stopped
36  *
37  * I have only tested this with .WAV and .AIFF sound file formats. Both seem
38  * to work fine.
39  *
40  * plans for the future:
41  * - use the midi api to define sounds (or drum beat etc) in xml and have
42  * Ant play them back
43  *
44  */

45
46 public class SoundTask extends Task {
47
48     private BuildAlert success = null;
49     private BuildAlert fail = null;
50
51     /**
52      * add a sound when the build succeeds
53      * @return a BuildAlert to be configured
54      */

55     public BuildAlert createSuccess() {
56         success = new BuildAlert();
57         return success;
58     }
59
60     /**
61      * add a sound when the build fails
62      * @return a BuildAlert to be configured
63      */

64     public BuildAlert createFail() {
65         fail = new BuildAlert();
66         return fail;
67      }
68
69     /** Constructor for SoundTask. */
70     public SoundTask() {
71     }
72
73     /**
74      * Initialize the task.
75      */

76     public void init() {
77     }
78
79     /**
80      * Execute the task.
81      */

82     public void execute() {
83
84         AntSoundPlayer soundPlayer = new AntSoundPlayer();
85
86         if (success == null) {
87             log("No nested success element found.", Project.MSG_WARN);
88         } else {
89             soundPlayer.addBuildSuccessfulSound(success.getSource(),
90               success.getLoops(), success.getDuration());
91         }
92
93         if (fail == null) {
94             log("No nested failure element found.", Project.MSG_WARN);
95         } else {
96             soundPlayer.addBuildFailedSound(fail.getSource(),
97               fail.getLoops(), fail.getDuration());
98         }
99
100         getProject().addBuildListener(soundPlayer);
101
102     }
103
104     /**
105      * A class to be extended by any BuildAlert's that require the output
106      * of sound.
107      */

108     public class BuildAlert {
109         private File JavaDoc source = null;
110         private int loops = 0;
111         private Long JavaDoc duration = null;
112
113         /**
114          * Sets the duration in milliseconds the file should be played; optional.
115          * @param duration the duration in millisconds
116          */

117         public void setDuration(Long JavaDoc duration) {
118             this.duration = duration;
119         }
120
121         /**
122          * Sets the location of the file to get the audio; required.
123          *
124          * @param source the name of a sound-file directory or of the audio file
125          */

126         public void setSource(File JavaDoc source) {
127             this.source = source;
128         }
129
130         /**
131          * Sets the number of times the source file should be played; optional.
132          *
133          * @param loops the number of loops to play the source file
134          */

135         public void setLoops(int loops) {
136             this.loops = loops;
137         }
138
139         /**
140          * Gets the location of the file to get the audio.
141          * @return the file location
142          */

143         public File JavaDoc getSource() {
144             File JavaDoc nofile = null;
145             // Check if source is a directory
146
if (source.exists()) {
147                 if (source.isDirectory()) {
148                     // get the list of files in the dir
149
String JavaDoc[] entries = source.list();
150                     Vector JavaDoc files = new Vector JavaDoc();
151                     for (int i = 0; i < entries.length; i++) {
152                         File JavaDoc f = new File JavaDoc(source, entries[i]);
153                         if (f.isFile()) {
154                             files.addElement(f);
155                         }
156                     }
157                     if (files.size() < 1) {
158                         throw new BuildException("No files found in directory " + source);
159                     }
160                     int numfiles = files.size();
161                     // get a random number between 0 and the number of files
162
Random JavaDoc rn = new Random JavaDoc();
163                     int x = rn.nextInt(numfiles);
164                     // set the source to the file at that location
165
this.source = (File JavaDoc) files.elementAt(x);
166                 }
167             } else {
168                 log(source + ": invalid path.", Project.MSG_WARN);
169                 this.source = nofile;
170             }
171             return this.source;
172         }
173
174         /**
175          * Sets the number of times the source file should be played.
176          *
177          * @return the number of loops to play the source file
178          */

179         public int getLoops() {
180             return this.loops;
181         }
182
183         /**
184          * Gets the duration in milliseconds the file should be played.
185          * @return the duration in milliseconds
186          */

187         public Long JavaDoc getDuration() {
188             return this.duration;
189         }
190     }
191 }
192
193
Popular Tags