001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: AudioClip.java,v $
023   Revision 1.7  2004/05/12 19:10:34  markl
024   comment block updates
025
026   Revision 1.6  2004/01/23 00:03:58  markl
027   javadoc corrections
028
029   Revision 1.5  2003/01/19 09:50:52  markl
030   Javadoc & comment header updates.
031
032   Revision 1.4  2001/03/12 09:27:51  markl
033   Source code and Javadoc cleanup.
034
035   Revision 1.3  1999/04/19 05:30:29  markl
036   Added getName()/setName() methods.
037
038   Revision 1.2  1999/01/10 01:00:58  markl
039   added GPL header & RCS tag
040   ----------------------------------------------------------------------------
041*/
042
043package kiwi.ui;
044
045import java.net.URL;
046import java.io.*;
047import sun.audio.*;
048
049/** This class represents an audio clip. The audio data is restricted to the
050  * 8000Hz, single-channel u-law format. The class relies on the undocumented
051  * <b>sun.audio</b> package and thus may not be portable.
052  * <p>
053  * <code>AudioClip</code>s may be read from streams, from files, or loaded as
054  * system resources using a <code>ResourceManager</code> or
055  * <code>ResourceLoader</code>.
056  *
057  * @see kiwi.util.ResourceManager#getSound
058  * @see kiwi.util.ResourceLoader#getResourceAsURL
059  *
060  * @author Mark Lindner
061  */
062
063public class AudioClip implements java.applet.AudioClip
064  {
065  static int length;
066  private AudioData audioData;
067  private AudioDataStream audioStream = null;
068  private ContinuousAudioDataStream cAudioStream = null;
069  private String name = "Untitled";
070
071  /** Construct a new <code>AudioClip</code>.
072    *
073    * @param url The location of the audio data.
074    *
075    * @exception java.io.IOException If there is a problem reading from the
076    * specified URL.
077    */
078
079  public AudioClip(URL url) throws java.io.IOException
080    {
081    audioData = new AudioStream(url.openStream()).getData();
082    }
083
084  /** Construct a new <code>AudioClip</code>.
085    *
086    * @param file The name of the file that contains the audio data.
087    *
088    * @exception java.io.IOException If there is a problem reading from the
089    * specified file.
090    */
091
092  public AudioClip(String file) throws IOException
093    {
094    this(new FileInputStream(file));
095    }
096
097  /** Construct a new <code>AudioClip</code>.
098    *
099    * @param stream The stream to read the audio data from.
100    *
101    * @exception java.io.IOException If there is a problem reading from the
102    * specified stream.
103    */
104
105  public AudioClip(InputStream stream) throws IOException
106    {
107    AudioStream audioStream = new AudioStream(stream);
108    audioData = audioStream.getData();
109    }
110
111  /** Play the audio clip. */
112
113  public void play()
114    {
115    audioStream = new AudioDataStream(audioData);
116    AudioPlayer.player.start(audioStream);
117    }
118
119  /** Play the audio clip continuously. */
120
121  public void loop()
122    {
123    cAudioStream = new ContinuousAudioDataStream(audioData);
124    AudioPlayer.player.start(cAudioStream);
125    }
126
127  /** Stop playing the audio clip. */
128
129  public void stop()
130    {
131    if(audioStream != null)
132      AudioPlayer.player.stop(audioStream);
133    if(cAudioStream != null)
134      AudioPlayer.player.stop(cAudioStream);
135    }
136
137  /** Set the audio clip's name.
138   *
139   * @param name The name.
140   */
141  
142  public void setName(String name)
143    {
144    if(name != null)
145      this.name = name;
146    }
147
148  /** Get the audio clip's name.
149   *
150   * @return The name.
151   */
152  
153  public String getName()
154    {
155    return(name);
156    }
157
158  /** Get a string representation of this object.
159   *
160   * @return The name of the audio clip.
161   */
162  
163  public String toString()
164    {
165    return(getName());
166    }
167
168  }
169
170/* end of source file */