summaryrefslogtreecommitdiff
path: root/songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2007-01-08 23:53:00 +0000
committerBjörn Stenberg <bjorn@haxx.se>2007-01-08 23:53:00 +0000
commit7039a05147b8bbfc829babea1c65bd436450b505 (patch)
tree4ba555eb84ed97b72b0575034d5b0530a393713e /songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java
parent6d4c19707ef95942e323cbdc89fbbfdbe45e7cc5 (diff)
downloadrockbox-7039a05147b8bbfc829babea1c65bd436450b505.tar.gz
rockbox-7039a05147b8bbfc829babea1c65bd436450b505.zip
Splitting out songdbj
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@11953 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java')
-rw-r--r--songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java242
1 files changed, 0 insertions, 242 deletions
diff --git a/songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java b/songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java
deleted file mode 100644
index 45a31e46e1..0000000000
--- a/songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java
+++ /dev/null
@@ -1,242 +0,0 @@
1/*
2 * 11/19/04 1.0 moved to LGPL.
3 *-----------------------------------------------------------------------
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *----------------------------------------------------------------------
18 */
19
20package javazoom.jl.player.advanced;
21
22import java.io.InputStream;
23
24import javazoom.jl.decoder.Bitstream;
25import javazoom.jl.decoder.BitstreamException;
26import javazoom.jl.decoder.Decoder;
27import javazoom.jl.decoder.Header;
28import javazoom.jl.decoder.JavaLayerException;
29import javazoom.jl.decoder.SampleBuffer;
30import javazoom.jl.player.AudioDevice;
31import javazoom.jl.player.FactoryRegistry;
32
33/**
34 * a hybrid of javazoom.jl.player.Player tweeked to include <code>play(startFrame, endFrame)</code>
35 * hopefully this will be included in the api
36 */
37public class AdvancedPlayer
38{
39 /** The MPEG audio bitstream.*/
40 private Bitstream bitstream;
41 /** The MPEG audio decoder. */
42 private Decoder decoder;
43 /** The AudioDevice the audio samples are written to. */
44 private AudioDevice audio;
45 /** Has the player been closed? */
46 private boolean closed = false;
47 /** Has the player played back all frames from the stream? */
48 private boolean complete = false;
49 private int lastPosition = 0;
50 /** Listener for the playback process */
51 private PlaybackListener listener;
52
53 /**
54 * Creates a new <code>Player</code> instance.
55 */
56 public AdvancedPlayer(InputStream stream) throws JavaLayerException
57 {
58 this(stream, null);
59 }
60
61 public AdvancedPlayer(InputStream stream, AudioDevice device) throws JavaLayerException
62 {
63 bitstream = new Bitstream(stream);
64
65 if (device!=null) audio = device;
66 else audio = FactoryRegistry.systemRegistry().createAudioDevice();
67 audio.open(decoder = new Decoder());
68 }
69
70 public void play() throws JavaLayerException
71 {
72 play(Integer.MAX_VALUE);
73 }
74
75 /**
76 * Plays a number of MPEG audio frames.
77 *
78 * @param frames The number of frames to play.
79 * @return true if the last frame was played, or false if there are
80 * more frames.
81 */
82 public boolean play(int frames) throws JavaLayerException
83 {
84 boolean ret = true;
85
86 // report to listener
87 if(listener != null) listener.playbackStarted(createEvent(PlaybackEvent.STARTED));
88
89 while (frames-- > 0 && ret)
90 {
91 ret = decodeFrame();
92 }
93
94// if (!ret)
95 {
96 // last frame, ensure all data flushed to the audio device.
97 AudioDevice out = audio;
98 if (out != null)
99 {
100// System.out.println(audio.getPosition());
101 out.flush();
102// System.out.println(audio.getPosition());
103 synchronized (this)
104 {
105 complete = (!closed);
106 close();
107 }
108
109 // report to listener
110 if(listener != null) listener.playbackFinished(createEvent(out, PlaybackEvent.STOPPED));
111 }
112 }
113 return ret;
114 }
115
116 /**
117 * Cloases this player. Any audio currently playing is stopped
118 * immediately.
119 */
120 public synchronized void close()
121 {
122 AudioDevice out = audio;
123 if (out != null)
124 {
125 closed = true;
126 audio = null;
127 // this may fail, so ensure object state is set up before
128 // calling this method.
129 out.close();
130 lastPosition = out.getPosition();
131 try
132 {
133 bitstream.close();
134 }
135 catch (BitstreamException ex)
136 {}
137 }
138 }
139
140 /**
141 * Decodes a single frame.
142 *
143 * @return true if there are no more frames to decode, false otherwise.
144 */
145 protected boolean decodeFrame() throws JavaLayerException
146 {
147 try
148 {
149 AudioDevice out = audio;
150 if (out == null) return false;
151
152 Header h = bitstream.readFrame();
153 if (h == null) return false;
154
155 // sample buffer set when decoder constructed
156 SampleBuffer output = (SampleBuffer) decoder.decodeFrame(h, bitstream);
157
158 synchronized (this)
159 {
160 out = audio;
161 if(out != null)
162 {
163 out.write(output.getBuffer(), 0, output.getBufferLength());
164 }
165 }
166
167 bitstream.closeFrame();
168 }
169 catch (RuntimeException ex)
170 {
171 throw new JavaLayerException("Exception decoding audio frame", ex);
172 }
173 return true;
174 }
175
176 /**
177 * skips over a single frame
178 * @return false if there are no more frames to decode, true otherwise.
179 */
180 protected boolean skipFrame() throws JavaLayerException
181 {
182 Header h = bitstream.readFrame();
183 if (h == null) return false;
184 bitstream.closeFrame();
185 return true;
186 }
187
188 /**
189 * Plays a range of MPEG audio frames
190 * @param start The first frame to play
191 * @param end The last frame to play
192 * @return true if the last frame was played, or false if there are more frames.
193 */
194 public boolean play(final int start, final int end) throws JavaLayerException
195 {
196 boolean ret = true;
197 int offset = start;
198 while (offset-- > 0 && ret) ret = skipFrame();
199 return play(end - start);
200 }
201
202 /**
203 * Constructs a <code>PlaybackEvent</code>
204 */
205 private PlaybackEvent createEvent(int id)
206 {
207 return createEvent(audio, id);
208 }
209
210 /**
211 * Constructs a <code>PlaybackEvent</code>
212 */
213 private PlaybackEvent createEvent(AudioDevice dev, int id)
214 {
215 return new PlaybackEvent(this, id, dev.getPosition());
216 }
217
218 /**
219 * sets the <code>PlaybackListener</code>
220 */
221 public void setPlayBackListener(PlaybackListener listener)
222 {
223 this.listener = listener;
224 }
225
226 /**
227 * gets the <code>PlaybackListener</code>
228 */
229 public PlaybackListener getPlayBackListener()
230 {
231 return listener;
232 }
233
234 /**
235 * closes the player and notifies <code>PlaybackListener</code>
236 */
237 public void stop()
238 {
239 listener.playbackFinished(createEvent(PlaybackEvent.STOPPED));
240 close();
241 }
242} \ No newline at end of file