KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SoundArea


1 /*
2  * @(#)SoundArea.java 1.17 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)SoundArea.java 1.17 06/02/22
39  */

40
41 import java.awt.Graphics JavaDoc;
42 import java.applet.AudioClip JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 /**
47  * An audio feedback ImageArea class.
48  * This class extends the basic ImageArea Class to play a sound when
49  * the user enters the area.
50  *
51  * @author Jim Graham
52  * @author Chuck McManis
53  * @version 1.17, 02/22/06
54  */

55 class SoundArea extends ImageMapArea {
56     /** The URL of the sound to be played. */
57     URL JavaDoc sound;
58     AudioClip JavaDoc soundData = null;
59     boolean hasPlayed;
60     boolean isReady = false;
61     long lastExit = 0;
62     final static int HYSTERESIS = 1500;
63
64     /**
65      * The argument is the URL of the sound to be played.
66      */

67     public void handleArg(String JavaDoc arg) {
68     try {
69         sound = new URL JavaDoc(parent.getDocumentBase(), arg);
70     } catch (MalformedURLException JavaDoc e) {
71         sound = null;
72     }
73     hasPlayed = false;
74     }
75
76     /**
77      * The applet thread calls the getMedia() method when the applet
78      * is started.
79      */

80     public void getMedia() {
81     if (sound != null && soundData == null) {
82         soundData = parent.getAudioClip(sound);
83     }
84     if (soundData == null) {
85         System.out.println("SoundArea: Unable to load data "+sound);
86     }
87     isReady = true;
88     }
89
90     /**
91      * The enter method is called when the mouse enters the area.
92      * The sound is played if the mouse has been outside of the
93      * area for more then the delay indicated by HYSTERESIS.
94      */

95     public void enter() {
96     // is the sound sample loaded?
97
if (! isReady) {
98         parent.showStatus("Loading media file...");
99         return;
100     }
101
102     /*
103      * So we entered the selection region, play the sound if
104      * we need to. Track the mouse entering and exiting the
105      * the selection box. If it doesn't stay out for more than
106      * "HYSTERESIS" millis, then don't re-play the sound.
107      */

108     long now = System.currentTimeMillis();
109     if (Math.abs(now - lastExit) < HYSTERESIS) {
110         // if within the window pretend that it was played.
111
hasPlayed = true;
112             return;
113     }
114
115     // Else play the sound.
116
if (! hasPlayed && (soundData != null)) {
117         hasPlayed = true;
118         soundData.play();
119     }
120     }
121
122     /**
123      * The exit method is called when the mouse leaves the area.
124      */

125     public void exit() {
126     if (hasPlayed) {
127         hasPlayed = false;
128         lastExit = System.currentTimeMillis(); // note the time of exit
129
}
130     }
131 }
132
Popular Tags