home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / audio / AudioPlayer.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.5 KB  |  123 lines

  1. /*
  2.  * @(#)AudioPlayer.java    1.29 96/02/03 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994, 1995 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. package sun.audio;
  21.  
  22. import java.util.Vector;
  23. import java.util.Enumeration;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.FileOutputStream;
  28.  
  29. /**
  30.  * This class provides an interface to play audio streams.
  31.  *
  32.  * To play an audio stream use:
  33.  * <pre>
  34.  *    AudioPlayer.player.start(audiostream);
  35.  * </pre>
  36.  * To stop playing an audio stream use:
  37.  * <pre>
  38.  *    AudioPlayer.player.stop(audiostream);
  39.  * </pre>
  40.  * To play an audio stream from a URL use:
  41.  * <pre>
  42.  *    AudioStream audiostream = new AudioStream(url.openStream());
  43.  *    AudioPlayer.player.start(audiostream);
  44.  * </pre>
  45.  * To play a continuous sound you first have to
  46.  * create an AudioData instance and use it to construct a
  47.  * ContinuousAudioDataStream.
  48.  * For example:
  49.  * <pre>
  50.  *    AudoData data = new AudioStream(url.openStream()).getData();
  51.  *    ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
  52.  *    AudioPlayer.player.stop(audiostream);
  53.  * </pre>
  54.  *
  55.  * @see AudioData
  56.  * @see AudioDataStream
  57.  * @see AudioDevice
  58.  * @see AudioStream
  59.  * @author Arthur van Hoff, Thomas Ball
  60.  * @version     1.29, 02/03/96
  61.  */
  62. public
  63. class AudioPlayer extends Thread {
  64.  
  65.     private AudioDevice devAudio;
  66.     
  67.     /**
  68.      * The default audio player. This audio player is initialized
  69.      * automatically.
  70.      */
  71.     public static final AudioPlayer player = new AudioPlayer();
  72.  
  73.     private static ThreadGroup getAudioThreadGroup() {
  74.     ThreadGroup g = currentThread().getThreadGroup();
  75.     while (g.getParent() != null) {
  76.         g = g.getParent();
  77.     }
  78.     return g;
  79.     }
  80.  
  81.     /**
  82.      * Construct an AudioPlayer.
  83.      */
  84.     private AudioPlayer() {
  85.     super(getAudioThreadGroup(), "Audio Player");
  86.     devAudio = AudioDevice.device;
  87.     setPriority(MAX_PRIORITY);
  88.     setDaemon(true);
  89.     start();
  90.     }
  91.  
  92.     /**
  93.      * Start playing a stream. The stream will continue to play
  94.      * until the stream runs out of data, or it is stopped.
  95.      * @see AudioPlayer#stop
  96.      */
  97.     public synchronized void start(InputStream in) {
  98.     devAudio.openChannel(in);
  99.     notify();
  100.     }
  101.  
  102.     /**
  103.      * Stop playing a stream. The stream will stop playing,
  104.      * nothing happens if the stream wasn't playing in the
  105.      * first place.
  106.      * @see AudioPlayer#start
  107.      */
  108.     public synchronized void stop(InputStream in) {
  109.     devAudio.closeChannel(in);
  110.     }
  111.  
  112.     /**
  113.      * Main mixing loop. This is called automatically when the AudioPlayer
  114.      * is created.
  115.      */
  116.     public void run() {
  117.     devAudio.open();
  118.     devAudio.play();
  119.     devAudio.close();
  120.     System.out.println("audio player exit");
  121.     }
  122. }
  123.