2
* 11/19/04 1.0 moved to LGPL.
3
* 29/01/00 Initial version. mdm@techie.com
4
*-----------------------------------------------------------------------
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Library General Public License as published
7
* by the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public
16
* License along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
*----------------------------------------------------------------------
21
package javazoom.jl.player;
23
import java.applet.Applet;
24
import java.io.IOException;
25
import java.io.InputStream;
28
import javazoom.jl.decoder.JavaLayerException;
31
* A simple applet that plays an MPEG audio file. The URL (relative to the
32
* document base) is passed as the "audioURL" parameter.
37
public class PlayerApplet extends Applet implements Runnable {
38
static public final String AUDIO_PARAMETER = "audioURL";
41
* The Player used to play the MPEG audio file.
43
private Player player = null;
46
* The thread that runs the player.
48
private Thread playerThread = null;
50
private String fileName = null;
53
* Retrieves the <code>AudioDevice</code> instance that will be used to
54
* sound the audio data.
56
* @return an audio device instance that will be used to sound the audio
59
protected AudioDevice getAudioDevice() throws JavaLayerException {
60
return FactoryRegistry.systemRegistry().createAudioDevice();
64
* Retrieves the InputStream that provides the MPEG audio stream data.
66
* @return an InputStream from which the MPEG audio data is read, or null if
69
protected InputStream getAudioStream() {
70
InputStream in = null;
73
URL url = getAudioURL();
75
in = url.openStream();
76
} catch (IOException ex) {
77
System.err.println(ex);
82
protected String getAudioFileName() {
83
String urlString = fileName;
84
if (urlString == null) {
85
urlString = getParameter(AUDIO_PARAMETER);
90
protected URL getAudioURL() {
91
String urlString = getAudioFileName();
93
if (urlString != null) {
95
url = new URL(getDocumentBase(), urlString);
96
} catch (Exception ex) {
97
System.err.println(ex);
104
* Sets the URL of the audio stream to play.
106
public void setFileName(String name) {
110
public String getFileName() {
115
* Stops the audio player. If the player is already stopped this method is a
118
protected void stopPlayer() throws JavaLayerException {
119
if (player != null) {
127
* Decompresses audio data from an InputStream and plays it back through an
128
* AudioDevice. The playback is run on a newly created thread.
131
* The InputStream that provides the MPEG audio data.
133
* The AudioDevice to use to sound the decompressed data.
135
* @throws JavaLayerException
136
* if there was a problem decoding or playing the audio data.
138
protected void play(InputStream in, AudioDevice dev)
139
throws JavaLayerException {
142
if (in != null && dev != null) {
143
player = new Player(in, dev);
144
playerThread = createPlayerThread();
145
playerThread.start();
150
* Creates a new thread used to run the audio player.
152
* @return A new Thread that, once started, runs the audio player.
154
protected Thread createPlayerThread() {
155
return new Thread(this, "Audio player thread");
159
* Initializes this applet.
165
* Starts this applet. An input stream and audio device are created and
166
* passed to the play() method.
168
public void start() {
169
String name = getAudioFileName();
171
InputStream in = getAudioStream();
172
AudioDevice dev = getAudioDevice();
174
} catch (JavaLayerException ex) {
175
synchronized (System.err) {
176
System.err.println("Unable to play " + name);
177
ex.printStackTrace(System.err);
183
* Stops this applet. If audio is currently playing, it is stopped.
188
} catch (JavaLayerException ex) {
189
System.err.println(ex);
193
public void destroy() {
197
* The run method for the audio player thread. Simply calls play() on the
198
* player to play the entire stream.
201
if (player != null) {
204
} catch (JavaLayerException ex) {
205
System.err.println("Problem playing audio: " + ex);