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.io.InputStream;
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;
33
* The <code>Player</code> class implements a simple player for playback of an
40
// REVIEW: the audio device should not be opened until the
41
// first MPEG audio frame has been decoded.
44
* The current frame number.
46
private int frame = 0;
49
* The MPEG audio bitstream.
51
// javac blank final bug.
52
/* final */private Bitstream bitstream;
55
* The MPEG audio decoder.
57
/* final */private Decoder decoder;
60
* The AudioDevice the audio samples are written to.
62
private AudioDevice audio;
65
* Has the player been closed?
67
private boolean closed = false;
70
* Has the player played back all frames from the stream?
72
private boolean complete = false;
74
private int lastPosition = 0;
77
* Creates a new <code>Player</code> instance.
79
public Player(InputStream stream) throws JavaLayerException {
83
public Player(InputStream stream, AudioDevice device)
84
throws JavaLayerException {
85
bitstream = new Bitstream(stream);
86
decoder = new Decoder();
91
FactoryRegistry r = FactoryRegistry.systemRegistry();
92
audio = r.createAudioDevice();
97
public void play() throws JavaLayerException {
98
play(Integer.MAX_VALUE);
102
* Plays a number of MPEG audio frames.
105
* The number of frames to play.
106
* @return true if the last frame was played, or false if there are more
109
public boolean play(int frames) throws JavaLayerException {
112
while (frames-- > 0 && ret) {
117
// last frame, ensure all data flushed to the audio device.
118
AudioDevice out = audio;
121
synchronized (this) {
122
complete = (!closed);
131
* Cloases this player. Any audio currently playing is stopped immediately.
133
public synchronized void close() {
134
AudioDevice out = audio;
138
// this may fail, so ensure object state is set up before
139
// calling this method.
141
lastPosition = out.getPosition();
144
} catch (BitstreamException ex) {
150
* Returns the completed status of this player.
152
* @return true if all available MPEG audio frames have been decoded, or
155
public synchronized boolean isComplete() {
160
* Retrieves the position in milliseconds of the current audio sample being
161
* played. This method delegates to the <code>
162
* AudioDevice</code> that is
163
* used by this player to sound the decoded audio samples.
165
public int getPosition() {
166
int position = lastPosition;
168
AudioDevice out = audio;
170
position = out.getPosition();
176
* Decodes a single frame.
178
* @return true if there are no more frames to decode, false otherwise.
180
protected boolean decodeFrame() throws JavaLayerException {
182
AudioDevice out = audio;
186
Header h = bitstream.readFrame();
191
// sample buffer set when decoder constructed
192
SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h,
195
synchronized (this) {
198
out.write(output.getBuffer(), 0, output.getBufferLength());
202
bitstream.closeFrame();
203
} catch (RuntimeException ex) {
204
throw new JavaLayerException("Exception decoding audio frame", ex);
207
* catch (IOException ex) { System.out.println("exception decoding audio
208
* frame: "+ex); return false; } catch (BitstreamException bitex) {
209
* System.out.println("exception decoding audio frame: "+bitex); return
210
* false; } catch (DecoderException decex) {
211
* System.out.println("exception decoding audio frame: "+decex); return