1 36 37 40 41 import java.awt.Graphics ; 42 import java.applet.AudioClip ; 43 import java.net.URL ; 44 import java.net.MalformedURLException ; 45 46 55 class SoundArea extends ImageMapArea { 56 57 URL sound; 58 AudioClip soundData = null; 59 boolean hasPlayed; 60 boolean isReady = false; 61 long lastExit = 0; 62 final static int HYSTERESIS = 1500; 63 64 67 public void handleArg(String arg) { 68 try { 69 sound = new URL (parent.getDocumentBase(), arg); 70 } catch (MalformedURLException e) { 71 sound = null; 72 } 73 hasPlayed = false; 74 } 75 76 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 95 public void enter() { 96 if (! isReady) { 98 parent.showStatus("Loading media file..."); 99 return; 100 } 101 102 108 long now = System.currentTimeMillis(); 109 if (Math.abs(now - lastExit) < HYSTERESIS) { 110 hasPlayed = true; 112 return; 113 } 114 115 if (! hasPlayed && (soundData != null)) { 117 hasPlayed = true; 118 soundData.play(); 119 } 120 } 121 122 125 public void exit() { 126 if (hasPlayed) { 127 hasPlayed = false; 128 lastExit = System.currentTimeMillis(); } 130 } 131 } 132 | Popular Tags |