home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / sun / audio / AudioPlayer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  1.9 KB  |  56 lines

  1. package sun.audio;
  2.  
  3. import java.io.InputStream;
  4.  
  5. public class AudioPlayer extends Thread {
  6.    private AudioDevice devAudio;
  7.    public static final AudioPlayer player = new AudioPlayer();
  8.  
  9.    private static ThreadGroup getAudioThreadGroup() {
  10.       ThreadGroup g;
  11.       for(g = Thread.currentThread().getThreadGroup(); g.getParent() != null && g.getParent().getParent() != null; g = g.getParent()) {
  12.       }
  13.  
  14.       return g;
  15.    }
  16.  
  17.    private AudioPlayer() {
  18.       super(getAudioThreadGroup(), "Audio Player");
  19.       this.devAudio = AudioDevice.device;
  20.       ((Thread)this).setPriority(10);
  21.       ((Thread)this).setDaemon(true);
  22.       ((Thread)this).start();
  23.    }
  24.  
  25.    public synchronized void start(InputStream in) {
  26.       this.devAudio.openChannel(in);
  27.       this.notify();
  28.    }
  29.  
  30.    public synchronized void stop(InputStream in) {
  31.       this.devAudio.closeChannel(in);
  32.    }
  33.  
  34.    private synchronized void waitForData() throws InterruptedException {
  35.       this.devAudio.close();
  36.       this.wait();
  37.       this.devAudio.open();
  38.    }
  39.  
  40.    public void run() {
  41.       if (this.devAudio.openChannels() == 0) {
  42.          try {
  43.             this.waitForData();
  44.          } catch (InterruptedException var1) {
  45.             this.devAudio.closeStreams();
  46.             return;
  47.          }
  48.       }
  49.  
  50.       this.devAudio.open();
  51.       this.devAudio.play();
  52.       this.devAudio.close();
  53.       System.out.println("audio player exit");
  54.    }
  55. }
  56.