diff options
Diffstat (limited to 'songdbj/javazoom/jl/player/Player.java')
-rw-r--r-- | songdbj/javazoom/jl/player/Player.java | 251 |
1 files changed, 0 insertions, 251 deletions
diff --git a/songdbj/javazoom/jl/player/Player.java b/songdbj/javazoom/jl/player/Player.java deleted file mode 100644 index 32fb1f3051..0000000000 --- a/songdbj/javazoom/jl/player/Player.java +++ /dev/null | |||
@@ -1,251 +0,0 @@ | |||
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.io.InputStream; | ||
24 | |||
25 | import javazoom.jl.decoder.Bitstream; | ||
26 | import javazoom.jl.decoder.BitstreamException; | ||
27 | import javazoom.jl.decoder.Decoder; | ||
28 | import javazoom.jl.decoder.Header; | ||
29 | import javazoom.jl.decoder.JavaLayerException; | ||
30 | import javazoom.jl.decoder.SampleBuffer; | ||
31 | |||
32 | /** | ||
33 | * The <code>Player</code> class implements a simple player for playback | ||
34 | * of an MPEG audio stream. | ||
35 | * | ||
36 | * @author Mat McGowan | ||
37 | * @since 0.0.8 | ||
38 | */ | ||
39 | |||
40 | // REVIEW: the audio device should not be opened until the | ||
41 | // first MPEG audio frame has been decoded. | ||
42 | public class Player | ||
43 | { | ||
44 | /** | ||
45 | * The current frame number. | ||
46 | */ | ||
47 | private int frame = 0; | ||
48 | |||
49 | /** | ||
50 | * The MPEG audio bitstream. | ||
51 | */ | ||
52 | // javac blank final bug. | ||
53 | /*final*/ private Bitstream bitstream; | ||
54 | |||
55 | /** | ||
56 | * The MPEG audio decoder. | ||
57 | */ | ||
58 | /*final*/ private Decoder decoder; | ||
59 | |||
60 | /** | ||
61 | * The AudioDevice the audio samples are written to. | ||
62 | */ | ||
63 | private AudioDevice audio; | ||
64 | |||
65 | /** | ||
66 | * Has the player been closed? | ||
67 | */ | ||
68 | private boolean closed = false; | ||
69 | |||
70 | /** | ||
71 | * Has the player played back all frames from the stream? | ||
72 | */ | ||
73 | private boolean complete = false; | ||
74 | |||
75 | private int lastPosition = 0; | ||
76 | |||
77 | /** | ||
78 | * Creates a new <code>Player</code> instance. | ||
79 | */ | ||
80 | public Player(InputStream stream) throws JavaLayerException | ||
81 | { | ||
82 | this(stream, null); | ||
83 | } | ||
84 | |||
85 | public Player(InputStream stream, AudioDevice device) throws JavaLayerException | ||
86 | { | ||
87 | bitstream = new Bitstream(stream); | ||
88 | decoder = new Decoder(); | ||
89 | |||
90 | if (device!=null) | ||
91 | { | ||
92 | audio = device; | ||
93 | } | ||
94 | else | ||
95 | { | ||
96 | FactoryRegistry r = FactoryRegistry.systemRegistry(); | ||
97 | audio = r.createAudioDevice(); | ||
98 | } | ||
99 | audio.open(decoder); | ||
100 | } | ||
101 | |||
102 | public void play() throws JavaLayerException | ||
103 | { | ||
104 | play(Integer.MAX_VALUE); | ||
105 | } | ||
106 | |||
107 | /** | ||
108 | * Plays a number of MPEG audio frames. | ||
109 | * | ||
110 | * @param frames The number of frames to play. | ||
111 | * @return true if the last frame was played, or false if there are | ||
112 | * more frames. | ||
113 | */ | ||
114 | public boolean play(int frames) throws JavaLayerException | ||
115 | { | ||
116 | boolean ret = true; | ||
117 | |||
118 | while (frames-- > 0 && ret) | ||
119 | { | ||
120 | ret = decodeFrame(); | ||
121 | } | ||
122 | |||
123 | if (!ret) | ||
124 | { | ||
125 | // last frame, ensure all data flushed to the audio device. | ||
126 | AudioDevice out = audio; | ||
127 | if (out!=null) | ||
128 | { | ||
129 | out.flush(); | ||
130 | synchronized (this) | ||
131 | { | ||
132 | complete = (!closed); | ||
133 | close(); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | return ret; | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * Cloases this player. Any audio currently playing is stopped | ||
142 | * immediately. | ||
143 | */ | ||
144 | public synchronized void close() | ||
145 | { | ||
146 | AudioDevice out = audio; | ||
147 | if (out!=null) | ||
148 | { | ||
149 | closed = true; | ||
150 | audio = null; | ||
151 | // this may fail, so ensure object state is set up before | ||
152 | // calling this method. | ||
153 | out.close(); | ||
154 | lastPosition = out.getPosition(); | ||
155 | try | ||
156 | { | ||
157 | bitstream.close(); | ||
158 | } | ||
159 | catch (BitstreamException ex) | ||
160 | { | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * Returns the completed status of this player. | ||
167 | * | ||
168 | * @return true if all available MPEG audio frames have been | ||
169 | * decoded, or false otherwise. | ||
170 | */ | ||
171 | public synchronized boolean isComplete() | ||
172 | { | ||
173 | return complete; | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * Retrieves the position in milliseconds of the current audio | ||
178 | * sample being played. This method delegates to the <code> | ||
179 | * AudioDevice</code> that is used by this player to sound | ||
180 | * the decoded audio samples. | ||
181 | */ | ||
182 | public int getPosition() | ||
183 | { | ||
184 | int position = lastPosition; | ||
185 | |||
186 | AudioDevice out = audio; | ||
187 | if (out!=null) | ||
188 | { | ||
189 | position = out.getPosition(); | ||
190 | } | ||
191 | return position; | ||
192 | } | ||
193 | |||
194 | /** | ||
195 | * Decodes a single frame. | ||
196 | * | ||
197 | * @return true if there are no more frames to decode, false otherwise. | ||
198 | */ | ||
199 | protected boolean decodeFrame() throws JavaLayerException | ||
200 | { | ||
201 | try | ||
202 | { | ||
203 | AudioDevice out = audio; | ||
204 | if (out==null) | ||
205 | return false; | ||
206 | |||
207 | Header h = bitstream.readFrame(); | ||
208 | |||
209 | if (h==null) | ||
210 | return false; | ||
211 | |||
212 | // sample buffer set when decoder constructed | ||
213 | SampleBuffer output = (SampleBuffer)decoder.decodeFrame(h, bitstream); | ||
214 | |||
215 | synchronized (this) | ||
216 | { | ||
217 | out = audio; | ||
218 | if (out!=null) | ||
219 | { | ||
220 | out.write(output.getBuffer(), 0, output.getBufferLength()); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | bitstream.closeFrame(); | ||
225 | } | ||
226 | catch (RuntimeException ex) | ||
227 | { | ||
228 | throw new JavaLayerException("Exception decoding audio frame", ex); | ||
229 | } | ||
230 | /* | ||
231 | catch (IOException ex) | ||
232 | { | ||
233 | System.out.println("exception decoding audio frame: "+ex); | ||
234 | return false; | ||
235 | } | ||
236 | catch (BitstreamException bitex) | ||
237 | { | ||
238 | System.out.println("exception decoding audio frame: "+bitex); | ||
239 | return false; | ||
240 | } | ||
241 | catch (DecoderException decex) | ||
242 | { | ||
243 | System.out.println("exception decoding audio frame: "+decex); | ||
244 | return false; | ||
245 | } | ||
246 | */ | ||
247 | return true; | ||
248 | } | ||
249 | |||
250 | |||
251 | } | ||