KickJava   Java API By Example, From Geeks To Geeks.

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


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 // ant includes
22
import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.sound.sampled.AudioFormat JavaDoc;
25 import javax.sound.sampled.AudioInputStream JavaDoc;
26 import javax.sound.sampled.AudioSystem JavaDoc;
27 import javax.sound.sampled.Clip JavaDoc;
28 import javax.sound.sampled.DataLine JavaDoc;
29 import javax.sound.sampled.Line JavaDoc;
30 import javax.sound.sampled.LineEvent JavaDoc;
31 import javax.sound.sampled.LineListener JavaDoc;
32 import javax.sound.sampled.LineUnavailableException JavaDoc;
33 import javax.sound.sampled.UnsupportedAudioFileException JavaDoc;
34 import org.apache.tools.ant.BuildEvent;
35 import org.apache.tools.ant.BuildListener;
36 import org.apache.tools.ant.Project;
37
38
39
40 /**
41  * This class is designed to be used by any AntTask that requires audio output.
42  *
43  * It implements the BuildListener interface to listen for BuildEvents and could
44  * be easily extended to provide audio output upon any specific build events occuring.
45  *
46  * I have only tested this with .WAV and .AIFF sound file formats. Both seem to work fine.
47  *
48  */

49
50 public class AntSoundPlayer implements LineListener JavaDoc, BuildListener {
51
52     private File JavaDoc fileSuccess = null;
53     private int loopsSuccess = 0;
54     private Long JavaDoc durationSuccess = null;
55
56     private File JavaDoc fileFail = null;
57     private int loopsFail = 0;
58     private Long JavaDoc durationFail = null;
59
60     /** Constructor for AntSoundPlayer. */
61     public AntSoundPlayer() {
62     }
63
64     /**
65      * @param file the location of the audio file to be played when the
66      * build is successful
67      * @param loops the number of times the file should be played when
68      * the build is successful
69      * @param duration the number of milliseconds the file should be
70      * played when the build is successful
71      */

72     public void addBuildSuccessfulSound(File JavaDoc file, int loops, Long JavaDoc duration) {
73         this.fileSuccess = file;
74         this.loopsSuccess = loops;
75         this.durationSuccess = duration;
76     }
77
78
79     /**
80      * @param fileFail the location of the audio file to be played
81      * when the build fails
82      * @param loopsFail the number of times the file should be played
83      * when the build is fails
84      * @param durationFail the number of milliseconds the file should be
85      * played when the build fails
86      */

87     public void addBuildFailedSound(File JavaDoc fileFail, int loopsFail, Long JavaDoc durationFail) {
88         this.fileFail = fileFail;
89         this.loopsFail = loopsFail;
90         this.durationFail = durationFail;
91     }
92
93     /**
94      * Plays the file for duration milliseconds or loops.
95      */

96     private void play(Project project, File JavaDoc file, int loops, Long JavaDoc duration) {
97
98         Clip JavaDoc audioClip = null;
99
100         AudioInputStream JavaDoc audioInputStream = null;
101
102
103         try {
104             audioInputStream = AudioSystem.getAudioInputStream(file);
105         } catch (UnsupportedAudioFileException JavaDoc uafe) {
106             project.log("Audio format is not yet supported: "
107                 + uafe.getMessage());
108         } catch (IOException JavaDoc ioe) {
109             ioe.printStackTrace();
110         }
111
112         if (audioInputStream != null) {
113             AudioFormat JavaDoc format = audioInputStream.getFormat();
114             DataLine.Info JavaDoc info = new DataLine.Info JavaDoc(Clip JavaDoc.class, format,
115                                              AudioSystem.NOT_SPECIFIED);
116             try {
117                 audioClip = (Clip JavaDoc) AudioSystem.getLine(info);
118                 audioClip.addLineListener(this);
119                 audioClip.open(audioInputStream);
120             } catch (LineUnavailableException JavaDoc e) {
121                 project.log("The sound device is currently unavailable");
122                 return;
123             } catch (IOException JavaDoc e) {
124                 e.printStackTrace();
125             }
126
127             if (duration != null) {
128                 playClip(audioClip, duration.longValue());
129             } else {
130                 playClip(audioClip, loops);
131             }
132             audioClip.drain();
133             audioClip.close();
134         } else {
135             project.log("Can't get data from file " + file.getName());
136         }
137     }
138
139     private void playClip(Clip JavaDoc clip, int loops) {
140
141         clip.loop(loops);
142         while (clip.isRunning()) {
143             // Empty block
144
}
145     }
146
147     private void playClip(Clip JavaDoc clip, long duration) {
148         clip.loop(Clip.LOOP_CONTINUOUSLY);
149         try {
150             Thread.sleep(duration);
151         } catch (InterruptedException JavaDoc e) {
152             // Ignore Exception
153
}
154     }
155
156     /**
157      * This is implemented to listen for any line events and closes the
158      * clip if required.
159      * @param event the line event to follow
160      */

161     public void update(LineEvent JavaDoc event) {
162         if (event.getType().equals(LineEvent.Type.STOP)) {
163             Line JavaDoc line = event.getLine();
164             line.close();
165         } else if (event.getType().equals(LineEvent.Type.CLOSE)) {
166             /*
167              * There is a bug in JavaSound 0.90 (jdk1.3beta).
168              * It prevents correct termination of the VM.
169              * So we have to exit ourselves.
170              */

171             //System.exit(0);
172
}
173     }
174
175
176     /**
177      * Fired before any targets are started.
178      * @param event ignored
179      */

180     public void buildStarted(BuildEvent event) {
181     }
182
183     /**
184      * Fired after the last target has finished. This event
185      * will still be thrown if an error occurred during the build.
186      * @param event the build finished event.
187      * @see BuildEvent#getException()
188      */

189     public void buildFinished(BuildEvent event) {
190         if (event.getException() == null && fileSuccess != null) {
191             // build successfull!
192
play(event.getProject(), fileSuccess, loopsSuccess, durationSuccess);
193         } else if (event.getException() != null && fileFail != null) {
194             play(event.getProject(), fileFail, loopsFail, durationFail);
195         }
196     }
197
198     /**
199      * Fired when a target is started.
200      * @param event ignored.
201      * @see BuildEvent#getTarget()
202      */

203     public void targetStarted(BuildEvent event) {
204     }
205
206     /**
207      * Fired when a target has finished. This event will
208      * still be thrown if an error occurred during the build.
209      * @param event ignored.
210      * @see BuildEvent#getException()
211      */

212     public void targetFinished(BuildEvent event) {
213     }
214
215     /**
216      * Fired when a task is started.
217      * @param event ignored.
218      * @see BuildEvent#getTask()
219      */

220     public void taskStarted(BuildEvent event) {
221     }
222
223     /**
224      * Fired when a task has finished. This event will still
225      * be throw if an error occurred during the build.
226      * @param event ignored.
227      * @see BuildEvent#getException()
228      */

229     public void taskFinished(BuildEvent event) {
230     }
231
232     /**
233      * Fired whenever a message is logged.
234      * @param event the build event
235      * @see BuildEvent#getMessage()
236      * @see BuildEvent#getPriority()
237      */

238     public void messageLogged(BuildEvent event) {
239     }
240 }
241
242
Popular Tags