//------------------------------------------------------------------------------ // playau.java - plays a .au sound // Copyright 1997, All rights reserved // Version: 1.6 // Author: Jon McCain // Created: 7/14/96 // Modified: 10/9/97,added more paramters //------------------------------------------------------------------------------ //Parameters: sound - name of the sound file (au) to play // playtime - number of frames to show, controls how long the // image is animated. // imgstrip - Image strip with frames to show (default=playau.gif) // numframes - number of frames in the image strip (default=4) // framedelay - delay time between each frame (default=200ms) // note: changing this affects the playtime parameter // autoplay - 1=play sound as soon as applet starts (default=1) // 0=wait until mouse click to play the sound import java.io.InputStream; import java.awt.*; import java.net.*; public class playau extends java.applet.Applet implements Runnable { Thread playit = null; MediaTracker tracker; String imagename; Image imgstrip; String sound; int frame,numframes,framedelay; int elapsed,playtime; int autoplay; boolean clicked; public String getAppletInfo() { return "Au Player 1.6 By Jon McCain"; } // Read the parameters public void init() { sound = getParameter("sound"); String at = getParameter("playtime"); playtime = (at != null) ? Integer.valueOf(at).intValue() : 10; imagename = getParameter("imgstrip"); if (imagename == null) imagename = "playau.gif"; at = getParameter("numframes"); numframes = (at != null) ? Integer.valueOf(at).intValue() : 4; at = getParameter("framedelay"); framedelay = (at != null) ? Integer.valueOf(at).intValue() : 200; at = getParameter("autoplay"); autoplay = (at != null) ? Integer.valueOf(at).intValue() : 1; clicked = false; // start loading the images of the speaker tracker = new MediaTracker(this); imgstrip = getImage(getDocumentBase(), imagename); tracker.addImage(imgstrip, 0); } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); //Wait until image loaded. try { tracker.waitForAll(); } catch (InterruptedException e) {} frame = 0; repaint(); if (autoplay == 1 || clicked) { //start a thread to play the sound play(getCodeBase(), sound); //Now cycle the image through the frames a certain number of times //Hopefully user set playtime just right so we stay in this loop //until the sound finishes. elapsed = 0; while (playit != null) { if (++frame >= numframes) frame = 0; repaint(); try { Thread.sleep(framedelay); } catch (InterruptedException e) { break; } if (++elapsed > playtime) playit = null; } } //go back to the very first frame frame = 0; repaint(); clicked = false; playit = null; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (imgstrip != null) { int stripWidth = imgstrip.getWidth(this); int stripHeight = imgstrip.getHeight(this); int imageWidth = stripWidth / numframes; g.clipRect(0, 0, imageWidth, stripHeight); g.drawImage(imgstrip, -frame*imageWidth, 0, this); } } public void start() { if (playit == null) { playit = new Thread(this); playit.start(); } } public void stop() { playit = null; } // allow a mouse click to play the sound again public boolean handleEvent(Event evt) { if (evt.id == Event.MOUSE_DOWN) { clicked = true; if (playit == null) { playit = new Thread(this); playit.start(); } return true; } else return super.handleEvent(evt); } }