diff options
Diffstat (limited to 'songdbj/javazoom/jl/player/PlayerApplet.java')
-rw-r--r-- | songdbj/javazoom/jl/player/PlayerApplet.java | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/songdbj/javazoom/jl/player/PlayerApplet.java b/songdbj/javazoom/jl/player/PlayerApplet.java new file mode 100644 index 0000000000..d7c7dc2ffc --- /dev/null +++ b/songdbj/javazoom/jl/player/PlayerApplet.java | |||
@@ -0,0 +1,246 @@ | |||
1 | /* | ||
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. | ||
9 | * | ||
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. | ||
14 | * | ||
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 | *---------------------------------------------------------------------- | ||
19 | */ | ||
20 | |||
21 | package javazoom.jl.player; | ||
22 | |||
23 | import java.applet.Applet; | ||
24 | import java.io.IOException; | ||
25 | import java.io.InputStream; | ||
26 | import java.net.URL; | ||
27 | |||
28 | import javazoom.jl.decoder.JavaLayerException; | ||
29 | |||
30 | /** | ||
31 | * A simple applet that plays an MPEG audio file. | ||
32 | * The URL (relative to the document base) | ||
33 | * is passed as the "audioURL" parameter. | ||
34 | * | ||
35 | * @author Mat McGowan | ||
36 | * @since 0.0.8 | ||
37 | */ | ||
38 | public class PlayerApplet extends Applet implements Runnable | ||
39 | { | ||
40 | static public final String AUDIO_PARAMETER = "audioURL"; | ||
41 | |||
42 | /** | ||
43 | * The Player used to play the MPEG audio file. | ||
44 | */ | ||
45 | private Player player = null; | ||
46 | |||
47 | /** | ||
48 | * The thread that runs the player. | ||
49 | */ | ||
50 | private Thread playerThread = null; | ||
51 | |||
52 | private String fileName = null; | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Retrieves the <code>AudioDevice</code> instance that will | ||
57 | * be used to sound the audio data. | ||
58 | * | ||
59 | * @return an audio device instance that will be used to | ||
60 | * sound the audio stream. | ||
61 | */ | ||
62 | protected AudioDevice getAudioDevice() throws JavaLayerException | ||
63 | { | ||
64 | return FactoryRegistry.systemRegistry().createAudioDevice(); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Retrieves the InputStream that provides the MPEG audio | ||
69 | * stream data. | ||
70 | * | ||
71 | * @return an InputStream from which the MPEG audio data | ||
72 | * is read, or null if an error occurs. | ||
73 | */ | ||
74 | protected InputStream getAudioStream() | ||
75 | { | ||
76 | InputStream in = null; | ||
77 | |||
78 | try | ||
79 | { | ||
80 | URL url = getAudioURL(); | ||
81 | if (url!=null) | ||
82 | in = url.openStream(); | ||
83 | } | ||
84 | catch (IOException ex) | ||
85 | { | ||
86 | System.err.println(ex); | ||
87 | } | ||
88 | return in; | ||
89 | } | ||
90 | |||
91 | protected String getAudioFileName() | ||
92 | { | ||
93 | String urlString = fileName; | ||
94 | if (urlString==null) | ||
95 | { | ||
96 | urlString = getParameter(AUDIO_PARAMETER); | ||
97 | } | ||
98 | return urlString; | ||
99 | } | ||
100 | |||
101 | protected URL getAudioURL() | ||
102 | { | ||
103 | String urlString = getAudioFileName(); | ||
104 | URL url = null; | ||
105 | if (urlString!=null) | ||
106 | { | ||
107 | try | ||
108 | { | ||
109 | url = new URL(getDocumentBase(), urlString); | ||
110 | } | ||
111 | catch (Exception ex) | ||
112 | { | ||
113 | System.err.println(ex); | ||
114 | } | ||
115 | } | ||
116 | return url; | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * Sets the URL of the audio stream to play. | ||
121 | */ | ||
122 | public void setFileName(String name) | ||
123 | { | ||
124 | fileName = name; | ||
125 | } | ||
126 | |||
127 | public String getFileName() | ||
128 | { | ||
129 | return fileName; | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * Stops the audio player. If the player is already stopped | ||
134 | * this method is a no-op. | ||
135 | */ | ||
136 | protected void stopPlayer() throws JavaLayerException | ||
137 | { | ||
138 | if (player!=null) | ||
139 | { | ||
140 | player.close(); | ||
141 | player = null; | ||
142 | playerThread = null; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Decompresses audio data from an InputStream and plays it | ||
148 | * back through an AudioDevice. The playback is run on a newly | ||
149 | * created thread. | ||
150 | * | ||
151 | * @param in The InputStream that provides the MPEG audio data. | ||
152 | * @param dev The AudioDevice to use to sound the decompressed data. | ||
153 | * | ||
154 | * @throws JavaLayerException if there was a problem decoding | ||
155 | * or playing the audio data. | ||
156 | */ | ||
157 | protected void play(InputStream in, AudioDevice dev) throws JavaLayerException | ||
158 | { | ||
159 | stopPlayer(); | ||
160 | |||
161 | if (in!=null && dev!=null) | ||
162 | { | ||
163 | player = new Player(in, dev); | ||
164 | playerThread = createPlayerThread(); | ||
165 | playerThread.start(); | ||
166 | } | ||
167 | } | ||
168 | |||
169 | /** | ||
170 | * Creates a new thread used to run the audio player. | ||
171 | * @return A new Thread that, once started, runs the audio player. | ||
172 | */ | ||
173 | protected Thread createPlayerThread() | ||
174 | { | ||
175 | return new Thread(this, "Audio player thread"); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Initializes this applet. | ||
180 | */ | ||
181 | public void init() | ||
182 | { | ||
183 | } | ||
184 | |||
185 | /** | ||
186 | * Starts this applet. An input stream and audio device | ||
187 | * are created and passed to the play() method. | ||
188 | */ | ||
189 | public void start() | ||
190 | { | ||
191 | String name = getAudioFileName(); | ||
192 | try | ||
193 | { | ||
194 | InputStream in = getAudioStream(); | ||
195 | AudioDevice dev = getAudioDevice(); | ||
196 | play(in, dev); | ||
197 | } | ||
198 | catch (JavaLayerException ex) | ||
199 | { | ||
200 | synchronized (System.err) | ||
201 | { | ||
202 | System.err.println("Unable to play "+name); | ||
203 | ex.printStackTrace(System.err); | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Stops this applet. If audio is currently playing, it is | ||
210 | * stopped. | ||
211 | */ | ||
212 | public void stop() | ||
213 | { | ||
214 | try | ||
215 | { | ||
216 | stopPlayer(); | ||
217 | } | ||
218 | catch (JavaLayerException ex) | ||
219 | { | ||
220 | System.err.println(ex); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | public void destroy() | ||
225 | { | ||
226 | } | ||
227 | |||
228 | /** | ||
229 | * The run method for the audio player thread. Simply calls | ||
230 | * play() on the player to play the entire stream. | ||
231 | */ | ||
232 | public void run() | ||
233 | { | ||
234 | if (player!=null) | ||
235 | { | ||
236 | try | ||
237 | { | ||
238 | player.play(); | ||
239 | } | ||
240 | catch (JavaLayerException ex) | ||
241 | { | ||
242 | System.err.println("Problem playing audio: "+ex); | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | } | ||