FileDocCategorySizeDatePackage
AntSoundPlayer.javaAPI DocApache Ant 1.707769Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.sound

AntSoundPlayer

public class AntSoundPlayer extends Object implements org.apache.tools.ant.BuildListener, LineListener
This class is designed to be used by any AntTask that requires audio output. It implements the BuildListener interface to listen for BuildEvents and could be easily extended to provide audio output upon any specific build events occuring. I have only tested this with .WAV and .AIFF sound file formats. Both seem to work fine.

Fields Summary
private File
fileSuccess
private int
loopsSuccess
private Long
durationSuccess
private File
fileFail
private int
loopsFail
private Long
durationFail
Constructors Summary
public AntSoundPlayer()
Constructor for AntSoundPlayer.


        
      
    
Methods Summary
public voidaddBuildFailedSound(java.io.File fileFail, int loopsFail, java.lang.Long durationFail)

param
fileFail the location of the audio file to be played when the build fails
param
loopsFail the number of times the file should be played when the build is fails
param
durationFail the number of milliseconds the file should be played when the build fails

        this.fileFail = fileFail;
        this.loopsFail = loopsFail;
        this.durationFail = durationFail;
    
public voidaddBuildSuccessfulSound(java.io.File file, int loops, java.lang.Long duration)

param
file the location of the audio file to be played when the build is successful
param
loops the number of times the file should be played when the build is successful
param
duration the number of milliseconds the file should be played when the build is successful

        this.fileSuccess = file;
        this.loopsSuccess = loops;
        this.durationSuccess = duration;
    
public voidbuildFinished(org.apache.tools.ant.BuildEvent event)
Fired after the last target has finished. This event will still be thrown if an error occurred during the build.

param
event the build finished event.
see
BuildEvent#getException()

        if (event.getException() == null && fileSuccess != null) {
            // build successfull!
            play(event.getProject(), fileSuccess, loopsSuccess, durationSuccess);
        } else if (event.getException() != null && fileFail != null) {
            play(event.getProject(), fileFail, loopsFail, durationFail);
        }
    
public voidbuildStarted(org.apache.tools.ant.BuildEvent event)
Fired before any targets are started.

param
event ignored

    
public voidmessageLogged(org.apache.tools.ant.BuildEvent event)
Fired whenever a message is logged.

param
event the build event
see
BuildEvent#getMessage()
see
BuildEvent#getPriority()

    
private voidplay(org.apache.tools.ant.Project project, java.io.File file, int loops, java.lang.Long duration)
Plays the file for duration milliseconds or loops.


        Clip audioClip = null;

        AudioInputStream audioInputStream = null;


        try {
            audioInputStream = AudioSystem.getAudioInputStream(file);
        } catch (UnsupportedAudioFileException uafe) {
            project.log("Audio format is not yet supported: "
                + uafe.getMessage());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        if (audioInputStream != null) {
            AudioFormat format = audioInputStream.getFormat();
            DataLine.Info   info = new DataLine.Info(Clip.class, format,
                                             AudioSystem.NOT_SPECIFIED);
            try {
                audioClip = (Clip) AudioSystem.getLine(info);
                audioClip.addLineListener(this);
                audioClip.open(audioInputStream);
            } catch (LineUnavailableException e) {
                project.log("The sound device is currently unavailable");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (duration != null) {
                playClip(audioClip, duration.longValue());
            } else {
                playClip(audioClip, loops);
            }
            audioClip.drain();
            audioClip.close();
        } else {
            project.log("Can't get data from file " + file.getName());
        }
    
private voidplayClip(javax.sound.sampled.Clip clip, int loops)


        clip.loop(loops);
        while (clip.isRunning()) {
            // Empty block
        }
    
private voidplayClip(javax.sound.sampled.Clip clip, long duration)

        clip.loop(Clip.LOOP_CONTINUOUSLY);
        try {
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            // Ignore Exception
        }
    
public voidtargetFinished(org.apache.tools.ant.BuildEvent event)
Fired when a target has finished. This event will still be thrown if an error occurred during the build.

param
event ignored.
see
BuildEvent#getException()

    
public voidtargetStarted(org.apache.tools.ant.BuildEvent event)
Fired when a target is started.

param
event ignored.
see
BuildEvent#getTarget()

    
public voidtaskFinished(org.apache.tools.ant.BuildEvent event)
Fired when a task has finished. This event will still be throw if an error occurred during the build.

param
event ignored.
see
BuildEvent#getException()

    
public voidtaskStarted(org.apache.tools.ant.BuildEvent event)
Fired when a task is started.

param
event ignored.
see
BuildEvent#getTask()

    
public voidupdate(javax.sound.sampled.LineEvent event)
This is implemented to listen for any line events and closes the clip if required.

param
event the line event to follow

        if (event.getType().equals(LineEvent.Type.STOP)) {
            Line line = event.getLine();
            line.close();
        } else if (event.getType().equals(LineEvent.Type.CLOSE)) {
            /*
             *  There is a bug in JavaSound 0.90 (jdk1.3beta).
             *  It prevents correct termination of the VM.
             *  So we have to exit ourselves.
             */
            //System.exit(0);
        }