1 18 19 package org.apache.tools.ant.taskdefs.optional.sound; 20 21 import java.io.File ; 23 import java.io.IOException ; 24 import javax.sound.sampled.AudioFormat ; 25 import javax.sound.sampled.AudioInputStream ; 26 import javax.sound.sampled.AudioSystem ; 27 import javax.sound.sampled.Clip ; 28 import javax.sound.sampled.DataLine ; 29 import javax.sound.sampled.Line ; 30 import javax.sound.sampled.LineEvent ; 31 import javax.sound.sampled.LineListener ; 32 import javax.sound.sampled.LineUnavailableException ; 33 import javax.sound.sampled.UnsupportedAudioFileException ; 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 49 50 public class AntSoundPlayer implements LineListener , BuildListener { 51 52 private File fileSuccess = null; 53 private int loopsSuccess = 0; 54 private Long durationSuccess = null; 55 56 private File fileFail = null; 57 private int loopsFail = 0; 58 private Long durationFail = null; 59 60 61 public AntSoundPlayer() { 62 } 63 64 72 public void addBuildSuccessfulSound(File file, int loops, Long duration) { 73 this.fileSuccess = file; 74 this.loopsSuccess = loops; 75 this.durationSuccess = duration; 76 } 77 78 79 87 public void addBuildFailedSound(File fileFail, int loopsFail, Long durationFail) { 88 this.fileFail = fileFail; 89 this.loopsFail = loopsFail; 90 this.durationFail = durationFail; 91 } 92 93 96 private void play(Project project, File file, int loops, Long duration) { 97 98 Clip audioClip = null; 99 100 AudioInputStream audioInputStream = null; 101 102 103 try { 104 audioInputStream = AudioSystem.getAudioInputStream(file); 105 } catch (UnsupportedAudioFileException uafe) { 106 project.log("Audio format is not yet supported: " 107 + uafe.getMessage()); 108 } catch (IOException ioe) { 109 ioe.printStackTrace(); 110 } 111 112 if (audioInputStream != null) { 113 AudioFormat format = audioInputStream.getFormat(); 114 DataLine.Info info = new DataLine.Info (Clip .class, format, 115 AudioSystem.NOT_SPECIFIED); 116 try { 117 audioClip = (Clip ) AudioSystem.getLine(info); 118 audioClip.addLineListener(this); 119 audioClip.open(audioInputStream); 120 } catch (LineUnavailableException e) { 121 project.log("The sound device is currently unavailable"); 122 return; 123 } catch (IOException 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 clip, int loops) { 140 141 clip.loop(loops); 142 while (clip.isRunning()) { 143 } 145 } 146 147 private void playClip(Clip clip, long duration) { 148 clip.loop(Clip.LOOP_CONTINUOUSLY); 149 try { 150 Thread.sleep(duration); 151 } catch (InterruptedException e) { 152 } 154 } 155 156 161 public void update(LineEvent event) { 162 if (event.getType().equals(LineEvent.Type.STOP)) { 163 Line line = event.getLine(); 164 line.close(); 165 } else if (event.getType().equals(LineEvent.Type.CLOSE)) { 166 171 } 173 } 174 175 176 180 public void buildStarted(BuildEvent event) { 181 } 182 183 189 public void buildFinished(BuildEvent event) { 190 if (event.getException() == null && fileSuccess != null) { 191 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 203 public void targetStarted(BuildEvent event) { 204 } 205 206 212 public void targetFinished(BuildEvent event) { 213 } 214 215 220 public void taskStarted(BuildEvent event) { 221 } 222 223 229 public void taskFinished(BuildEvent event) { 230 } 231 232 238 public void messageLogged(BuildEvent event) { 239 } 240 } 241 242 | Popular Tags |