home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 11.3 KB | 409 lines |
- package symantec.itools.multimedia;
-
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.MalformedURLException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Vector;
- import sun.audio.AudioStream;
- import sun.audio.AudioDataStream;
- import sun.audio.AudioPlayer;
- import sun.audio.AudioData;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.beans.PropertyChangeEvent;
-
- // 06/01/97 TG Updated to support Java 1.1
- // Made properties bound & constrained
- // 07/17/97 CAR marked fields transient as needed
- // implemented readObject to create Audio streams after deserialization
- // 07/17/97 LAB Layed out the code in accordance to the Good Bean Spec. Updated version tag
- // to 1.1.
- // 08/19/97 CAR changed getURLList to use the new URLlist vector
-
- /**
- * SoundPlayer component.
- * This component allows the playing of a list of sound clips
- * either serially or simultaneously. The sound information is
- * 8-bit 8000hz u-law encoded data.
- *
- * @version 1.1, July 25, 1997
- * @author Symantec
- */
- public class SoundPlayer implements java.io.Serializable
- {
- /**
- * A constant specifying that the sound should run forever.
- * Use with setRepeat().
- * @see #setRepeat
- */
- public static final int INFINITE = -1;
-
- /**
- * Constructs a SoundPlayer.
- * By default the sound clips will play serially (sync mode true),
- * and will play only once (repeat count == 1).
- */
- public SoundPlayer()
- {
- clips = new Vector();
- URLlist = new Vector();
- }
-
- /**
- * Sets whether to play the sound clips one after the other,
- * or all simultaneously.
- *
- * @param newSyncMode true to play the sound clips serially,
- * false to play them all simultaneously
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setSyncMode(boolean newSyncMode) throws PropertyVetoException
- {
- if (sync != newSyncMode)
- {
- Boolean oldSyncModeBoolean = new Boolean(sync);
- Boolean newSyncModeBoolean = new Boolean(newSyncMode);
-
- vetos.fireVetoableChange("SyncMode", oldSyncModeBoolean, newSyncModeBoolean);
-
- sync = newSyncMode;
- if (spt != null)
- spt.doSync(newSyncMode);
-
- changes.firePropertyChange("SyncMode", oldSyncModeBoolean, newSyncModeBoolean);
- }
- }
-
- /**
- * Returns the number of times the list of sound clips is played.
- */
- public boolean isSyncMode()
- {
- return sync;
- }
-
- /**
- * @deprecate
- * @see #isSyncMode
- */
- public boolean getSyncMode()
- {
- return isSyncMode();
- }
-
- /**
- * Sets the number of times the list of sound clips is played.
- *
- * @param c the number of times to play the list of sound clips,
- * or INFINITE to keep playing forever
- * @see #getRepeat
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setRepeat(int c) throws PropertyVetoException
- {
- if (repeatCt != c)
- {
- Integer newValue = new Integer(c);
- Integer oldValue = new Integer(repeatCt);
-
- vetos.fireVetoableChange("Repeat", oldValue, newValue);
-
- repeatCt = c;
-
- changes.firePropertyChange("Repeat", oldValue, newValue);
- }
- }
-
- /**
- * Gets the number of times the list of sound clips is played.
- * @see #setRepeat(int)
- */
- public int getRepeat()
- {
- return repeatCt;
- }
-
- /**
- * Initializes the list of sound clips using the given list of sound clip
- * data URLs. Any previously existing sound clip information is lost.
- * The files specified by the URLs contain u-law encoded 8-bit 8000hz data.
- * @param list a list of URLs of files containing sound clip data
- * @see #getURLList
- *
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setURLList(URL[] list) throws PropertyVetoException
- {
- clips.removeAllElements();
- URLlist.removeAllElements();
-
- URL[] oldValue = getURLList();
-
- vetos.fireVetoableChange("URLList", oldValue, list);
-
- for (int i = 0; i < list.length; ++i)
- addURL(list[i]);
-
- changes.firePropertyChange("URLList", oldValue, list);
- }
-
- /**
- * Gets the URL of each sound clip.
- * @see #setURLList
- */
- public URL[] getURLList()
- {
- /*
- int len = clips.size();
- URL[] ret = new URL[len];
- for (int i = 0; i < len; ++i)
- ret[i] = (URL)clips.elementAt(i);
- */
-
- URL[] ret = new URL[URLlist.size()];
- URLlist.copyInto(ret);
-
- return ret;
- }
-
- //
- // Methods
- //
-
- /**
- * Appends the sound clip data at the specified URL to the current list
- * of sound clips.
- * The specified file contains u-law encoded 8-bit 8000hz data.
- * @param u the URL of a file containing sound clip data
- */
- public void addURL(URL u)
- {
- InputStream in = null;
- AudioData data;
- try
- {
- try
- {
- URLConnection uc = u.openConnection();
- uc.setAllowUserInteraction(true);
- in = uc.getInputStream();
- AudioStream as = new AudioStream(in);
- clips.addElement(new SoundViewerItem(as.getData(),
- (int)(((double)as.getLength() / 7168.0) * 1000.0)));
- URLlist.addElement(u);
- }
- finally
- {
- if (in != null)
- in.close();
- }
- }
- catch (IOException e) { }
- }
-
- /**
- * Appends the sound clip data at the specified URL to the current list
- * of sound clips.
- * The specified file contains u-law encoded 8-bit 8000hz data.
- * @param url the URL of a file containing sound clip data
- */
- public void addStringURL(String url)
- {
- try
- {
- addURL(new URL(url));
- }
- catch (MalformedURLException e) {}
- }
-
- /**
- * Starts playing the sound clips.
- */
- public void play()
- {
- spt = new SoundViewerThread(clips, sync, repeatCt);
- spt.start();
- }
-
- /**
- * Immediately stops the playing of sound clips.
- */
- public void stop()
- {
- if (spt != null)
- spt.doStop();
- }
-
- /**
- * Stops the playing of sound clips after the specified number of
- * milliseconds.
- * @param #delay the number of milliseconds to wait before stopping
- * sound play
- */
- public void stop(int delay)
- {
- try
- {
- Thread.sleep(delay);
- }
- catch (InterruptedException ie) {}
- stop();
- }
-
- /**
- * Adds a listener for all event changes.
- * @param PropertyChangeListener listener the listener to add.
- * @see #removePropertyChangeListener
- */
- public void addPropertyChangeListener(PropertyChangeListener listener)
- {
- //super.addPropertyChangeListener(listener);
- changes.addPropertyChangeListener(listener);
- }
-
- /**
- * Removes a listener for all event changes.
- * @param PropertyChangeListener listener the listener to remove.
- * @see #addPropertyChangeListener
- */
- public void removePropertyChangeListener(PropertyChangeListener listener)
- {
- //super.removePropertyChangeListener(listener);
- changes.removePropertyChangeListener(listener);
- }
-
- /**
- * Adds a vetoable listener for all event changes.
- * @param VetoableChangeListener listener the listener to add.
- * @see #removeVetoableChangeListener
- */
- public void addVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.addVetoableChangeListener(listener);
- vetos.addVetoableChangeListener(listener);
- }
-
- /**
- * Removes a vetoable listener for all event changes.
- * @param VetoableChangeListener listener the listener to remove.
- * @see #addVetoableChangeListener
- */
- public void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.removeVetoableChangeListener(listener);
- vetos.removeVetoableChangeListener(listener);
- }
-
- private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException
- {
- stream.defaultReadObject();
-
- clips = new Vector();
- for (int i = 0; i < URLlist.size(); ++i)
- {
- InputStream in = null;
- AudioData data;
- try
- {
- try
- {
- URLConnection uc = ((URL) URLlist.elementAt(i)).openConnection();
- uc.setAllowUserInteraction(true);
- in = uc.getInputStream();
- AudioStream as = new AudioStream(in);
- clips.addElement(new SoundViewerItem(as.getData(),
- (int)(((double)as.getLength() / 7168.0) * 1000.0)));
- }
- finally
- {
- if (in != null)
- in.close();
- }
- } catch (IOException e) {}
- }
- }
-
- // Private members
- transient private Vector clips;
- transient private SoundViewerThread spt = null;
- private Vector URLlist;
- private boolean sync = true;
- private int repeatCt = 1;
-
- private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
- private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
- }
-
- class SoundViewerThread extends Thread
- {
- SoundViewerThread(Vector clips, boolean sync, int rct)
- {
- this.clips = clips;
- this.sync = sync;
- repeatCt = rct;
- }
-
- public void run()
- {
- while (repeatCt == -1 || repeatCt-- > 0)
- {
- int nClips = clips.size();
- for (int index = 0; index < nClips && !doEnd; ++index)
- {
- SoundViewerItem spi = (SoundViewerItem)clips.elementAt(index);
- curStream = new AudioDataStream(spi.data);
- AudioPlayer.player.start(curStream);
- if (sync)
- {
- try
- {
- Thread.sleep(spi.delay);
- }
- catch (InterruptedException ie) {}
- }
- }
- curStream = null;
- }
- doEnd = false;
- }
-
- void doSync(boolean f)
- {
- sync = f;
- }
-
- void doStop()
- {
- if (curStream != null)
- AudioPlayer.player.stop(curStream);
- doEnd = true;
- }
-
- private Vector clips;
- private boolean doEnd = false;
- private AudioDataStream curStream = null;
- private int repeatCt = 0;
- private boolean sync = true;
- }
-
- class SoundViewerItem
- {
- SoundViewerItem(AudioData data, int delay)
- {
- this.data = data;
- this.delay = delay;
- }
-
- AudioData data;
- int delay;
- }
-