home *** CD-ROM | disk | FTP | other *** search
/ Computer Life: Multimedia Mega Pac / Multimedia_Mega-Pac_Computer_Life_1996.iso / hotjava / demo / classes / audioite.jav < prev    next >
Text File  |  1995-05-19  |  3KB  |  133 lines

  1. /*
  2.  * @(#)AudioItem.java    1.26 95/03/22 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.io.InputStream;
  21. import java.util.Hashtable;
  22. import java.util.Enumeration;
  23. import java.util.StringTokenizer;
  24.  
  25. import awt.*;
  26. import browser.*;
  27. import browser.audio.*;
  28. import net.www.html.*;
  29.  
  30. /**
  31.  * A simple Item class to play an audio clip.
  32.  * @author James Gosling
  33.  */
  34.  
  35. public
  36. class AudioItem extends Applet {
  37.     /**
  38.      * The sounds to be played.
  39.      */
  40.     private String sounds;
  41.  
  42.     /**
  43.      * The index of the next sound in the sounds strings.
  44.      */
  45.     private int index;
  46.  
  47.     /**
  48.      * The currently playing audio stream, or null
  49.      * when no audio is playing.
  50.      */
  51.     private InputStream audio;
  52.  
  53.     /**
  54.      * Play the next sound. The sound URLS are obtained
  55.      * from the "snd" attribute. You can specify a list
  56.      * of them by seperating the sounds by '|'s.<p>
  57.      * Note that the URL is constructed relative to the
  58.      * documentURL, that is because the url is obtained
  59.      * from within the document
  60.      */
  61.     public void next() {
  62.     try {
  63.         stopPlaying(audio);
  64.         audio = null;
  65.         
  66.         String url = sounds;
  67.         if (sounds.indexOf('|') >= 0) {
  68.         int start = index;
  69.         if ((index = sounds.indexOf('|', index)) < 0) {
  70.             url = sounds.substring(start);
  71.             index = start;
  72.         } else {
  73.             url = sounds.substring(start, index++);
  74.         }
  75.         }
  76.         if (url.length() > 0) {
  77.         audio = getAudioStream(new URL(documentURL, url));
  78.         startPlaying(audio);
  79.         }
  80.     } catch(Exception e) {
  81.     }
  82.     }
  83.  
  84.     /**
  85.      * Initialize the applet. First resize it, then get the
  86.      * "snd" attribute.
  87.      */
  88.     public void init() {
  89.     resize(10, 12);
  90.  
  91.     sounds = getAttribute("snd");
  92.     if (sounds == null) {
  93.         sounds = "doc:/demo/audio/ding.au";
  94.     }
  95.     }
  96.  
  97.     /**
  98.      * When the applet is started play the next sound.
  99.      */
  100.     public void start() {
  101.     next();
  102.     }
  103.  
  104.     /**
  105.      * When the applet is stopped, stop playing the current sound.
  106.      */
  107.     public void stop() {
  108.     if (audio != null) {
  109.         stopPlaying(audio);
  110.         audio = null;
  111.     }
  112.     }
  113.  
  114.     /**
  115.      * When the user clicks in the applet, play the next sound.
  116.      */
  117.     public void mouseUp(int x, int y) {
  118.     next();
  119.     }
  120.  
  121.     /**
  122.      * Paint an audio icon.
  123.      */
  124.     public void paint(Graphics g) {
  125.     double f = ((double)(height - 1)) / ((width - 1) * 2);
  126.     int offset = height / 2;
  127.     for (int i = width - 1; i >= 0; i -= 3) {
  128.         int h = (int)(i * f);
  129.         g.drawLine(i, offset - h, i, offset + h);
  130.     }
  131.     }
  132. }
  133.