home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / SoundExample.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  138 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.*;
  18. import java.awt.*;
  19.  
  20. public class SoundExample extends Applet {
  21.     SoundList soundList;
  22.     String onceFile = "bark.au";
  23.     String loopFile = "train.au";
  24.     AudioClip onceClip;
  25.     AudioClip loopClip;
  26.  
  27.     Button playOnce;
  28.     Button startLoop;
  29.     Button stopLoop;
  30.     Button reload;
  31.  
  32.     boolean looping = false;
  33.  
  34.     public void init() {
  35.         playOnce = new Button("Bark!");
  36.         add(playOnce);
  37.  
  38.         startLoop = new Button("Start sound loop");
  39.         stopLoop = new Button("Stop sound loop");
  40.         stopLoop.disable();
  41.         add(startLoop);
  42.         add(stopLoop);
  43.  
  44.         reload = new Button("Reload sounds");
  45.         add(reload);
  46.  
  47.         validate();
  48.  
  49.         startLoadingSounds();
  50.     }
  51.  
  52.     void startLoadingSounds() {
  53.         //Start asynchronous sound loading.
  54.         soundList = new SoundList(this, getCodeBase());
  55.         soundList.startLoading(loopFile);
  56.         soundList.startLoading(onceFile);
  57.    }
  58.  
  59.     public void stop() {
  60.         //If one-time sound were long, we'd stop it here, too.
  61.         if (looping) {
  62.             loopClip.stop();    //Stop the sound loop.
  63.         }
  64.     }    
  65.  
  66.     public void start() {
  67.         if (looping) {
  68.             loopClip.loop();    //Restart the sound loop.
  69.         }
  70.     }    
  71.  
  72.     public boolean action(Event event, Object arg) {
  73.         //PLAY BUTTON
  74.         if (event.target == playOnce) {
  75.             if (onceClip == null) {
  76.                 //Try to get the AudioClip.
  77.                 onceClip = soundList.getClip(onceFile);
  78.             }
  79.  
  80.             if (onceClip != null) {  //If the sound is loaded:
  81.                 onceClip.play();     //Play it once.
  82.                 showStatus("Playing sound " + onceFile + ".");
  83.             } else {
  84.                 showStatus("Sound " + onceFile + " not loaded yet.");
  85.             }
  86.             return true;
  87.         }
  88.  
  89.         //START LOOP BUTTON
  90.         if (event.target == startLoop) {
  91.             if (loopClip == null) {
  92.                 //Try to get the AudioClip.
  93.                 loopClip = soundList.getClip(loopFile);
  94.             }
  95.  
  96.             if (loopClip != null) {  //If the sound is loaded:
  97.                 looping = true;
  98.                 loopClip.loop();     //Start the sound loop.
  99.                 stopLoop.enable();   //Enable stop button.
  100.                 startLoop.disable(); //Disable start button.
  101.                 showStatus("Playing sound " + loopFile + " continuously.");
  102.             } else {
  103.                 showStatus("Sound " + loopFile + " not loaded yet.");
  104.             }
  105.             return true;
  106.         }
  107.  
  108.         //STOP LOOP BUTTON
  109.         if (event.target == stopLoop) {
  110.             if (looping) {
  111.                 looping = false;
  112.                 loopClip.stop();    //Stop the sound loop.
  113.                 startLoop.enable(); //Enable start button.
  114.                 stopLoop.disable(); //Disable stop button.
  115.             }
  116.             showStatus("Stopped playing sound " + loopFile + ".");
  117.             return true;
  118.         }
  119.  
  120.         //RELOAD BUTTON
  121.         if (event.target == reload) {
  122.             if (looping) {          //Stop the sound loop.
  123.                 looping = false;
  124.                 loopClip.stop();
  125.                 startLoop.enable(); //Enable start button.
  126.                 stopLoop.disable(); //Disable stop button.
  127.             }
  128.             loopClip = null;        //Reset AudioClip to null.
  129.             onceClip = null;        //Reset AudioClip to null.
  130.             startLoadingSounds();
  131.             showStatus("Reloading all sounds.");
  132.             return true;
  133.         }
  134.  
  135.         return false; //some event I don't know about...
  136.     }
  137. }
  138.