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
22 //import java.io.InputStream;
\r
26 * The <code>Player</code> class implements a simple player for playback
\r
27 * of an MPEG audio stream.
\r
29 * @author Mat McGowan
\r
33 // REVIEW: the audio device should not be opened until the
\r
34 // first MPEG audio frame has been decoded.
\r
35 @LATTICE("B<DE,DE<ST,DE<HE,HE<ST,ST<FR")
\r
39 * The current frame number.
\r
41 @LOC("FR") private int frame = 0;
\r
44 * The MPEG audio bitstream.
\r
46 // javac blank final bug.
\r
47 /*final*/ @LOC("ST") private Bitstream bitstream;
\r
50 * The MPEG audio decoder.
\r
52 /*final*/ @LOC("DE") private Decoder decoder;
\r
55 * The AudioDevice the audio samples are written to.
\r
57 //private AudioDevice audio;
\r
60 * Has the player been closed?
\r
62 @LOC("B") private boolean closed = false;
\r
65 * Has the player played back all frames from the stream?
\r
67 @LOC("B") private boolean complete = false;
\r
69 @LOC("B") private int lastPosition = 0;
\r
72 * Creates a new <code>Player</code> instance.
\r
74 public Player(InputStream stream) throws JavaLayerException
\r
76 this(stream, null);
\r
80 public Player(InputStream stream, AudioDevice device) throws JavaLayerException
\r
82 bitstream = new Bitstream(stream);
\r
83 decoder = new Decoder();
\r
85 // if (device!=null)
\r
91 // FactoryRegistry r = FactoryRegistry.systemRegistry();
\r
92 // audio = r.createAudioDevice();
\r
95 device.open(decoder);
\r
99 public void play() throws JavaLayerException
\r
101 play(Integer.MAX_VALUE);
\r
105 * Plays a number of MPEG audio frames.
\r
107 * @param frames The number of frames to play.
\r
108 * @return true if the last frame was played, or false if there are
\r
111 @LATTICE("IN<T,IN*,THISLOC=T")
\r
113 public boolean play(@LOC("IN") int frames) throws JavaLayerException
\r
115 @LOC("IN") 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
142 * Cloases this player. Any audio currently playing is stopped
\r
146 public synchronized void close()
\r
149 AudioDevice out = audio;
\r
154 // this may fail, so ensure object state is set up before
\r
155 // calling this method.
\r
157 lastPosition = out.getPosition();
\r
162 catch (BitstreamException ex)
\r
171 * Returns the completed status of this player.
\r
173 * @return true if all available MPEG audio frames have been
\r
174 * decoded, or false otherwise.
\r
176 public synchronized boolean isComplete()
\r
182 * Retrieves the position in milliseconds of the current audio
\r
183 * sample being played. This method delegates to the <code>
\r
184 * AudioDevice</code> that is used by this player to sound
\r
185 * the decoded audio samples.
\r
187 public int getPosition()
\r
189 //int position = lastPosition;
\r
191 //AudioDevice out = audio;
\r
194 // position = out.getPosition();
\r
201 * Decodes a single frame.
\r
203 * @return true if there are no more frames to decode, false otherwise.
\r
205 @LATTICE("O<TH,THISLOC=TH")
\r
207 protected boolean decodeFrame() throws JavaLayerException
\r
211 //AudioDevice out = audio;
\r
215 Header h = bitstream.readFrame();
\r
220 // sample buffer set when decoder constructed
\r
221 @LOC("O") SampleBuffer output = (SampleBuffer)decoder.decodeFrame(h, bitstream);
\r
223 //synchronized (this)
\r
228 // out.write(output.getBuffer(), 0, output.getBufferLength());
\r
232 bitstream.closeFrame();
\r
234 catch (RuntimeException ex)
\r
236 throw new JavaLayerException("Exception decoding audio frame", ex);
\r
239 catch (IOException ex)
\r
241 System.out.println("exception decoding audio frame: "+ex);
\r
244 catch (BitstreamException bitex)
\r
246 System.out.println("exception decoding audio frame: "+bitex);
\r
249 catch (DecoderException decex)
\r
251 System.out.println("exception decoding audio frame: "+decex);
\r