2 * 11/19/04 1.0 moved to LGPL.
\r
3 * 29/01/00 Initial version. mdm@techie.com
\r
4 *-----------------------------------------------------------------------
\r
5 * This program is free software; you can redistribute it and/or modify
\r
6 * it under the terms of the GNU Library General Public License as published
\r
7 * by the Free Software Foundation; either version 2 of the License, or
\r
8 * (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU Library General Public License for more details.
\r
15 * You should have received a copy of the GNU Library General Public
\r
16 * License along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
18 *----------------------------------------------------------------------
\r
21 package javazoom.jl.player;
\r
23 import java.io.InputStream;
\r
25 import javazoom.jl.decoder.Bitstream;
\r
26 import javazoom.jl.decoder.BitstreamException;
\r
27 import javazoom.jl.decoder.Decoder;
\r
28 import javazoom.jl.decoder.Header;
\r
29 import javazoom.jl.decoder.JavaLayerException;
\r
30 import javazoom.jl.decoder.SampleBuffer;
\r
33 * The <code>Player</code> class implements a simple player for playback
\r
34 * of an MPEG audio stream.
\r
36 * @author Mat McGowan
\r
40 // REVIEW: the audio device should not be opened until the
\r
41 // first MPEG audio frame has been decoded.
\r
45 * The current frame number.
\r
47 private int frame = 0;
\r
50 * The MPEG audio bitstream.
\r
52 // javac blank final bug.
\r
53 /*final*/ private Bitstream bitstream;
\r
56 * The MPEG audio decoder.
\r
58 /*final*/ private Decoder decoder;
\r
61 * The AudioDevice the audio samples are written to.
\r
63 private AudioDevice audio;
\r
66 * Has the player been closed?
\r
68 private boolean closed = false;
\r
71 * Has the player played back all frames from the stream?
\r
73 private boolean complete = false;
\r
75 private int lastPosition = 0;
\r
78 * Creates a new <code>Player</code> instance.
\r
80 public Player(InputStream stream) throws JavaLayerException
\r
82 this(stream, null);
\r
85 public Player(InputStream stream, AudioDevice device) throws JavaLayerException
\r
87 bitstream = new Bitstream(stream);
\r
88 decoder = new Decoder();
\r
96 FactoryRegistry r = FactoryRegistry.systemRegistry();
\r
97 audio = r.createAudioDevice();
\r
99 audio.open(decoder);
\r
102 public void play() throws JavaLayerException
\r
104 play(Integer.MAX_VALUE);
\r
108 * Plays a number of MPEG audio frames.
\r
110 * @param frames The number of frames to play.
\r
111 * @return true if the last frame was played, or false if there are
\r
114 public boolean play(int frames) throws JavaLayerException
\r
116 boolean ret = true;
\r
118 while (frames-- > 0 && ret)
\r
120 ret = decodeFrame();
\r
125 // last frame, ensure all data flushed to the audio device.
\r
126 AudioDevice out = audio;
\r
130 synchronized (this)
\r
132 complete = (!closed);
\r
141 * Cloases this player. Any audio currently playing is stopped
\r
144 public synchronized void close()
\r
146 AudioDevice out = audio;
\r
151 // this may fail, so ensure object state is set up before
\r
152 // calling this method.
\r
154 lastPosition = out.getPosition();
\r
159 catch (BitstreamException ex)
\r
166 * Returns the completed status of this player.
\r
168 * @return true if all available MPEG audio frames have been
\r
169 * decoded, or false otherwise.
\r
171 public synchronized boolean isComplete()
\r
177 * Retrieves the position in milliseconds of the current audio
\r
178 * sample being played. This method delegates to the <code>
\r
179 * AudioDevice</code> that is used by this player to sound
\r
180 * the decoded audio samples.
\r
182 public int getPosition()
\r
184 int position = lastPosition;
\r
186 AudioDevice out = audio;
\r
189 position = out.getPosition();
\r
195 * Decodes a single frame.
\r
197 * @return true if there are no more frames to decode, false otherwise.
\r
199 protected boolean decodeFrame() throws JavaLayerException
\r
203 AudioDevice out = audio;
\r
207 Header h = bitstream.readFrame();
\r
212 // sample buffer set when decoder constructed
\r
213 SampleBuffer output = (SampleBuffer)decoder.decodeFrame(h, bitstream);
\r
215 synchronized (this)
\r
220 out.write(output.getBuffer(), 0, output.getBufferLength());
\r
224 bitstream.closeFrame();
\r
226 catch (RuntimeException ex)
\r
228 throw new JavaLayerException("Exception decoding audio frame", ex);
\r
231 catch (IOException ex)
\r
233 System.out.println("exception decoding audio frame: "+ex);
\r
236 catch (BitstreamException bitex)
\r
238 System.out.println("exception decoding audio frame: "+bitex);
\r
241 catch (DecoderException decex)
\r
243 System.out.println("exception decoding audio frame: "+decex);
\r