Board index » jbuilder » stopping an applet when html page is minimized
|
Thomas J. Barnett
JBuilder Developer |
stopping an applet when html page is minimized2004-06-26 07:40:34 AM jbuilder6 Hello Could somebody please look at the following code and explain why the applet does not stop (sound continues) when the browser it is displayed looses focus? And how the status bar message can be made to display longer? Many thanks J. import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.net.*; public class SoundPlayer extends JApplet { boolean isStandalone = false; String url; boolean loop; String status; URL source; AudioClip soundFile; //debug: StringBuffer buffer; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public SoundPlayer() { } //Initialize the applet public void init() { //debug: buffer = new StringBuffer(); addItem("initializing... "); try { url = this.getParameter("url", ""); } catch(Exception e) { e.printStackTrace(); } try { source = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } soundFile = this.getAudioClip(source); try { loop = Boolean.valueOf(this.getParameter("loop", "false")).booleanValue(); } catch(Exception e) { e.printStackTrace(); } try { status = this.getParameter("status", "message to display in status bar"); } catch(Exception e) { e.printStackTrace(); } try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Start the applet public void start() { //debug: addItem("starting... "); loop ? soundFile.loop() : soundFile.play(); } public void stop() { //debug: addItem("stopping... "); soundFile.stop(); } public void destroy() { //debug: addItem("preparing for unloading..."); } /*debug: void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); } public void paint(Graphics g) { //Draw the current string inside the rectangle. g.drawString(buffer.toString(), 5, 15); } */ //Component initialization private void jbInit() throws Exception { this.setSize(new Dimension(400,300)); } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { String[][] pinfo = { {"url", "String", "An absolute URL giving the location of the audio clip"}, {"loop", "boolean", "Whether the audio clip should loop. Default is false"}, {"status", "String", "Status bar message to be displayed"}, }; return pinfo; } //static initializer for setting look & feel static { try { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()) ; } catch(Exception e) { } } } |
