source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 7.3 KB
Line 
1/*
2 * Copyright 2000-2002,2004-2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.sound;
19
20// ant includes
21import java.io.File;
22import java.io.IOException;
23import javax.sound.sampled.AudioFormat;
24import javax.sound.sampled.AudioInputStream;
25import javax.sound.sampled.AudioSystem;
26import javax.sound.sampled.Clip;
27import javax.sound.sampled.DataLine;
28import javax.sound.sampled.Line;
29import javax.sound.sampled.LineEvent;
30import javax.sound.sampled.LineListener;
31import javax.sound.sampled.LineUnavailableException;
32import javax.sound.sampled.UnsupportedAudioFileException;
33import org.apache.tools.ant.BuildEvent;
34import org.apache.tools.ant.BuildListener;
35import org.apache.tools.ant.Project;
36
37
38
39/**
40 * This class is designed to be used by any AntTask that requires audio output.
41 *
42 * It implements the BuildListener interface to listen for BuildEvents and could
43 * be easily extended to provide audio output upon any specific build events occuring.
44 *
45 * I have only tested this with .WAV and .AIFF sound file formats. Both seem to work fine.
46 *
47 */
48
49public class AntSoundPlayer implements LineListener, BuildListener {
50
51 private File fileSuccess = null;
52 private int loopsSuccess = 0;
53 private Long durationSuccess = null;
54
55 private File fileFail = null;
56 private int loopsFail = 0;
57 private Long durationFail = null;
58
59 public AntSoundPlayer() {
60
61 }
62
63 /**
64 * @param file the location of the audio file to be played when the
65 * build is successful
66 * @param loops the number of times the file should be played when
67 * the build is successful
68 * @param duration the number of milliseconds the file should be
69 * played when the build is successful
70 */
71 public void addBuildSuccessfulSound(File file, int loops, Long duration) {
72 this.fileSuccess = file;
73 this.loopsSuccess = loops;
74 this.durationSuccess = duration;
75 }
76
77
78 /**
79 * @param fileFail the location of the audio file to be played
80 * when the build fails
81 * @param loopsFail the number of times the file should be played
82 * when the build is fails
83 * @param durationFail the number of milliseconds the file should be
84 * played when the build fails
85 */
86 public void addBuildFailedSound(File fileFail, int loopsFail, Long durationFail) {
87 this.fileFail = fileFail;
88 this.loopsFail = loopsFail;
89 this.durationFail = durationFail;
90 }
91
92 /**
93 * Plays the file for duration milliseconds or loops.
94 */
95 private void play(Project project, File file, int loops, Long duration) {
96
97 Clip audioClip = null;
98
99 AudioInputStream audioInputStream = null;
100
101
102 try {
103 audioInputStream = AudioSystem.getAudioInputStream(file);
104 } catch (UnsupportedAudioFileException uafe) {
105 project.log("Audio format is not yet supported: "
106 + uafe.getMessage());
107 } catch (IOException ioe) {
108 ioe.printStackTrace();
109 }
110
111 if (audioInputStream != null) {
112 AudioFormat format = audioInputStream.getFormat();
113 DataLine.Info info = new DataLine.Info(Clip.class, format,
114 AudioSystem.NOT_SPECIFIED);
115 try {
116 audioClip = (Clip) AudioSystem.getLine(info);
117 audioClip.addLineListener(this);
118 audioClip.open(audioInputStream);
119 } catch (LineUnavailableException e) {
120 project.log("The sound device is currently unavailable");
121 return;
122 } catch (IOException e) {
123 e.printStackTrace();
124 }
125
126 if (duration != null) {
127 playClip(audioClip, duration.longValue());
128 } else {
129 playClip(audioClip, loops);
130 }
131 audioClip.drain();
132 audioClip.close();
133 } else {
134 project.log("Can't get data from file " + file.getName());
135 }
136 }
137
138 private void playClip(Clip clip, int loops) {
139
140 clip.loop(loops);
141 while (clip.isRunning()) {
142 }
143 }
144
145 private void playClip(Clip clip, long duration) {
146 clip.loop(Clip.LOOP_CONTINUOUSLY);
147 try {
148 Thread.sleep(duration);
149 } catch (InterruptedException e) {
150 }
151 }
152
153 /**
154 * This is implemented to listen for any line events and closes the
155 * clip if required.
156 */
157 public void update(LineEvent event) {
158 if (event.getType().equals(LineEvent.Type.STOP)) {
159 Line line = event.getLine();
160 line.close();
161 } else if (event.getType().equals(LineEvent.Type.CLOSE)) {
162 /*
163 * There is a bug in JavaSound 0.90 (jdk1.3beta).
164 * It prevents correct termination of the VM.
165 * So we have to exit ourselves.
166 */
167 //System.exit(0);
168 }
169 }
170
171
172 /**
173 * Fired before any targets are started.
174 */
175 public void buildStarted(BuildEvent event) {
176 }
177
178 /**
179 * Fired after the last target has finished. This event
180 * will still be thrown if an error occurred during the build.
181 *
182 * @see BuildEvent#getException()
183 */
184 public void buildFinished(BuildEvent event) {
185 if (event.getException() == null && fileSuccess != null) {
186 // build successfull!
187 play(event.getProject(), fileSuccess, loopsSuccess, durationSuccess);
188 } else if (event.getException() != null && fileFail != null) {
189 play(event.getProject(), fileFail, loopsFail, durationFail);
190 }
191 }
192
193 /**
194 * Fired when a target is started.
195 *
196 * @see BuildEvent#getTarget()
197 */
198 public void targetStarted(BuildEvent event) {
199 }
200
201 /**
202 * Fired when a target has finished. This event will
203 * still be thrown if an error occurred during the build.
204 *
205 * @see BuildEvent#getException()
206 */
207 public void targetFinished(BuildEvent event) {
208 }
209
210 /**
211 * Fired when a task is started.
212 *
213 * @see BuildEvent#getTask()
214 */
215 public void taskStarted(BuildEvent event) {
216 }
217
218 /**
219 * Fired when a task has finished. This event will still
220 * be throw if an error occurred during the build.
221 *
222 * @see BuildEvent#getException()
223 */
224 public void taskFinished(BuildEvent event) {
225 }
226
227 /**
228 * Fired whenever a message is logged.
229 *
230 * @see BuildEvent#getMessage()
231 * @see BuildEvent#getPriority()
232 */
233 public void messageLogged(BuildEvent event) {
234 }
235}
236
Note: See TracBrowser for help on using the repository browser.