summaryrefslogtreecommitdiff
path: root/songdbj/javazoom/jl/player/advanced
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/javazoom/jl/player/advanced')
-rw-r--r--songdbj/javazoom/jl/player/advanced/AdvancedPlayer.java242
-rw-r--r--songdbj/javazoom/jl/player/advanced/PlaybackEvent.java51
-rw-r--r--songdbj/javazoom/jl/player/advanced/PlaybackListener.java30
-rw-r--r--songdbj/javazoom/jl/player/advanced/jlap.java116
4 files changed, 0 insertions, 439 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
diff --git a/songdbj/javazoom/jl/player/advanced/PlaybackEvent.java b/songdbj/javazoom/jl/player/advanced/PlaybackEvent.java
deleted file mode 100644
index 08e3cae958..0000000000
--- a/songdbj/javazoom/jl/player/advanced/PlaybackEvent.java
+++ /dev/null
@@ -1,51 +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
22/**
23 * An event which indicates a <code>Player</code> has performed an 'playback action'
24 * @author Paul Stanton (http://wanto.f2o.org/)
25 */
26public class PlaybackEvent
27{
28 public static int STOPPED = 1;
29 public static int STARTED = 2;
30
31 private AdvancedPlayer source;
32 private int frame;
33 private int id;
34
35 public PlaybackEvent(AdvancedPlayer source, int id, int frame)
36 {
37 this.id = id;
38 this.source = source;
39 this.frame = frame;
40 }
41
42 public int getId(){return id;}
43 public void setId(int id){this.id = id;}
44
45 public int getFrame(){return frame;}
46 public void setFrame(int frame){this.frame = frame;}
47
48 public AdvancedPlayer getSource(){return source;}
49 public void setSource(AdvancedPlayer source){this.source = source;}
50
51}
diff --git a/songdbj/javazoom/jl/player/advanced/PlaybackListener.java b/songdbj/javazoom/jl/player/advanced/PlaybackListener.java
deleted file mode 100644
index 9b042988b8..0000000000
--- a/songdbj/javazoom/jl/player/advanced/PlaybackListener.java
+++ /dev/null
@@ -1,30 +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
22/**
23 * Listener for javalayer Player playback
24 * @author Paul Stanton (http://wanto.f2o.org/)
25 */
26public abstract class PlaybackListener
27{
28 public void playbackStarted(PlaybackEvent evt){}
29 public void playbackFinished(PlaybackEvent evt){}
30}
diff --git a/songdbj/javazoom/jl/player/advanced/jlap.java b/songdbj/javazoom/jl/player/advanced/jlap.java
deleted file mode 100644
index beedea6716..0000000000
--- a/songdbj/javazoom/jl/player/advanced/jlap.java
+++ /dev/null
@@ -1,116 +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.BufferedInputStream;
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27
28import javazoom.jl.decoder.JavaLayerException;
29
30/**
31 * This class implements a sample player using Playback listener.
32 */
33public class jlap
34{
35
36 public static void main(String[] args)
37 {
38 jlap test = new jlap();
39 if (args.length != 1)
40 {
41 test.showUsage();
42 System.exit(0);
43 }
44 else
45 {
46 try
47 {
48 test.play(args[0]);
49 }
50 catch (Exception ex)
51 {
52 System.err.println(ex.getMessage());
53 System.exit(0);
54 }
55 }
56 }
57
58 public void play(String filename) throws JavaLayerException, IOException
59 {
60 InfoListener lst = new InfoListener();
61 playMp3(new File(filename), lst);
62 }
63
64 public void showUsage()
65 {
66 System.out.println("Usage: jla <filename>");
67 System.out.println("");
68 System.out.println(" e.g. : java javazoom.jl.player.advanced.jlap localfile.mp3");
69 }
70
71 public static AdvancedPlayer playMp3(File mp3, PlaybackListener listener) throws IOException, JavaLayerException
72 {
73 return playMp3(mp3, 0, Integer.MAX_VALUE, listener);
74 }
75
76 public static AdvancedPlayer playMp3(File mp3, int start, int end, PlaybackListener listener) throws IOException, JavaLayerException
77 {
78 return playMp3(new BufferedInputStream(new FileInputStream(mp3)), start, end, listener);
79 }
80
81 public static AdvancedPlayer playMp3(final InputStream is, final int start, final int end, PlaybackListener listener) throws JavaLayerException
82 {
83 final AdvancedPlayer player = new AdvancedPlayer(is);
84 player.setPlayBackListener(listener);
85 // run in new thread
86 new Thread()
87 {
88 public void run()
89 {
90 try
91 {
92 player.play(start, end);
93 }
94 catch (Exception e)
95 {
96 throw new RuntimeException(e.getMessage());
97 }
98 }
99 }.start();
100 return player;
101 }
102
103 public class InfoListener extends PlaybackListener
104 {
105 public void playbackStarted(PlaybackEvent evt)
106 {
107 System.out.println("Play started from frame " + evt.getFrame());
108 }
109
110 public void playbackFinished(PlaybackEvent evt)
111 {
112 System.out.println("Play completed at frame " + evt.getFrame());
113 System.exit(0);
114 }
115 }
116} \ No newline at end of file