home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / com / next / gt / AudioManager.java < prev    next >
Encoding:
Java Source  |  1998-04-20  |  1.4 KB  |  80 lines

  1. /**
  2.  * AudioManager.java
  3.  * @author Jeremy Hutchins, idea from: Bhagvan Kommadi
  4.  */
  5.  
  6. package com.next.gt;
  7.  
  8. import sun.audio.*;
  9. import java.io.*;
  10.  
  11.  /**
  12.   * Plays a audio files (.au format)
  13.   */
  14.  
  15. public class AudioManager extends java.applet.Applet
  16.                              implements Runnable
  17. {
  18.     private Thread _kicker = null;
  19.     private int _naptime = 5;
  20.     private boolean _keepRunning = true;
  21.     private boolean playAudioFiles = true;
  22.  
  23.    public void run()
  24.     {
  25.     while (_keepRunning)
  26.         {
  27.         try
  28.                  {
  29.                    _kicker.sleep(_naptime);
  30.                  }
  31.                 catch (InterruptedException e) {};
  32.     } 
  33.  
  34.    }
  35.  
  36.    public void start()
  37.    {
  38.     _kicker = new Thread(this);
  39.     _kicker.start();
  40.    }
  41.  
  42.    /**
  43.     * Open the audiostream and 
  44.     * play the audiofile by the AudioPlayer
  45.     */
  46.    public void play(String fileName)
  47.    {
  48.     AudioStream as;
  49.     if( playAudioFiles ) {
  50.         try
  51.         {
  52.         as = new AudioStream(new FileInputStream(fileName));
  53.         AudioPlayer.player.start(as);
  54.         }
  55.         catch (IOException e) {
  56.         System.err.println("Can't open audio stream, disabling audio..");
  57.         playAudioFiles = false;
  58.         }
  59.     }
  60.    }
  61.  
  62.    /**
  63.     * resume playing the sound
  64.     */
  65.    public void resumeplay(String fileName)
  66.    {
  67.       AudioPlayer.player.resume();
  68.    }
  69.  
  70.    /**
  71.     * suspend the player
  72.     */
  73.  
  74.    public void stopplay( )
  75.    {
  76.        AudioPlayer.player.suspend();
  77.    }
  78.  
  79. }
  80.