-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathPlatformPlayer.java
362 lines (284 loc) · 8.03 KB
/
PlatformPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
This file is part of FreeJ2ME.
FreeJ2ME is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeJ2ME is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeJ2ME. If not, see http://www.gnu.org/licenses/
*/
package org.recompile.mobile;
import java.io.InputStream;
import java.util.Vector;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.Control;
import javax.microedition.media.Controllable;
public class PlatformPlayer implements Player
{
private String contentType = "";
private audioplayer player;
private int state = Player.UNREALIZED;
private Vector<PlayerListener> listeners;
private Control[] controls;
public PlatformPlayer(InputStream stream, String type)
{
listeners = new Vector<PlayerListener>();
controls = new Control[3];
contentType = type;
if(type.equals("audio/midi") || type.equals("sp-midi") || type.equals("audio/spmidi"))
{
player = new midiPlayer(stream);
}
else
{
if(type.equals("audio/x-wav"))
{
player = new wavPlayer(stream);
}
else
{
System.out.println("No Player For: "+contentType);
player = new audioplayer();
}
}
controls[0] = new volumeControl();
controls[1] = new tempoControl();
controls[2] = new midiControl();
//System.out.println("media type: "+type);
}
public PlatformPlayer(String locator)
{
listeners = new Vector<PlayerListener>();
controls = new Control[3];
System.out.println("Player locator: "+locator);
}
public void close()
{
player.stop();
state = Player.CLOSED;
notifyListeners(PlayerListener.CLOSED, null);
}
public int getState()
{
if(player.isRunning()==false)
{
state = Player.PREFETCHED;
}
return state;
}
public void start()
{
//System.out.println("Play "+contentType);
if(Mobile.getPlatform().sound)
{
try
{
player.start();
}
catch (Exception e) { }
}
}
public void stop()
{
try
{
player.stop();
}
catch (Exception e) { }
}
public void addPlayerListener(PlayerListener playerListener)
{
//System.out.println("Add Player Listener");
listeners.add(playerListener);
}
public void removePlayerListener(PlayerListener playerListener)
{
listeners.remove(playerListener);
}
private void notifyListeners(String event, Object eventData)
{
for(int i=0; i<listeners.size(); i++)
{
listeners.get(i).playerUpdate(this, event, eventData);
}
}
public void deallocate() { state = Player.CLOSED; }
public String getContentType() { return contentType; }
public long getDuration() { return Player.TIME_UNKNOWN; }
public long getMediaTime() { return player.getMediaTime(); }
public void prefetch() { state = Player.PREFETCHED; }
public void realize() { state = Player.REALIZED; }
public void setLoopCount(int count) { player.setLoopCount(count); }
public long setMediaTime(long now) { return player.setMediaTime(now); }
// Controllable interface //
public Control getControl(String controlType)
{
if(controlType.equals("VolumeControl")) { return controls[0]; }
if(controlType.equals("TempoControl")) { return controls[1]; }
if(controlType.equals("MIDIControl")) { return controls[2]; }
if(controlType.equals("javax.microedition.media.control.VolumeControl")) { return controls[0]; }
if(controlType.equals("javax.microedition.media.control.TempoControl")) { return controls[1]; }
if(controlType.equals("javax.microedition.media.control.MIDIControl")) { return controls[2]; }
return null;
}
public Control[] getControls()
{
return controls;
}
// Players //
private class audioplayer
{
public void start() { }
public void stop() { }
public void setLoopCount(int count) { }
public long setMediaTime(long now) { return now; }
public long getMediaTime() { return 0; }
public boolean isRunning() { return false; }
}
private class midiPlayer extends audioplayer
{
private Sequencer midi;
private int loops = 0;
public midiPlayer(InputStream stream)
{
try
{
midi = MidiSystem.getSequencer();
midi.open();
midi.setSequence(stream);
state = Player.PREFETCHED;
}
catch (Exception e) { }
}
public void start()
{
midi.start();
state = Player.STARTED;
notifyListeners(PlayerListener.STARTED, new Long(0));
}
public void stop()
{
midi.stop();
state = Player.REALIZED;
}
public void setLoopCount(int count)
{
loops = count;
midi.setLoopCount(count);
}
public long setMediaTime(long now)
{
try
{
midi.setTickPosition(now);
}
catch (Exception e) { }
return now;
}
public long getMediaTime()
{
return 0;
}
public boolean isRunning()
{
return midi.isRunning();
}
}
private class wavPlayer extends audioplayer
{
private AudioInputStream wavStream;
private Clip wavClip;
private int loops = 0;
private Long time = new Long(0);
public wavPlayer(InputStream stream)
{
try
{
wavStream = AudioSystem.getAudioInputStream(stream);
wavClip = AudioSystem.getClip();
wavClip.open(wavStream);
state = Player.PREFETCHED;
}
catch (Exception e) { }
}
public void start()
{
if(isRunning()) { wavClip.setFramePosition(0); }
time = wavClip.getMicrosecondPosition();
wavClip.start();
state = Player.STARTED;
notifyListeners(PlayerListener.STARTED, time);
}
public void stop()
{
wavClip.stop();
time = wavClip.getMicrosecondPosition();
state = Player.PREFETCHED;
notifyListeners(PlayerListener.STOPPED, time);
}
public void setLoopCount(int count)
{
loops = count;
wavClip.loop(count);
}
public long setMediaTime(long now)
{
wavClip.setMicrosecondPosition(now);
return now;
}
public long getMediaTime()
{
return wavClip.getMicrosecondPosition();
}
public boolean isRunning()
{
return wavClip.isRunning();
}
}
// Controls //
private class midiControl implements javax.microedition.media.control.MIDIControl
{
public int[] getBankList(boolean custom) { return new int[]{}; }
public int getChannelVolume(int channel) { return 0; }
public java.lang.String getKeyName(int bank, int prog, int key) { return ""; }
public int[] getProgram(int channel) { return new int[]{}; }
public int[] getProgramList(int bank) { return new int[]{}; }
public java.lang.String getProgramName(int bank, int prog) { return ""; }
public boolean isBankQuerySupported() { return false; }
public int longMidiEvent(byte[] data, int offset, int length) { return 0; }
public void setChannelVolume(int channel, int volume) { }
public void setProgram(int channel, int bank, int program) { }
public void shortMidiEvent(int type, int data1, int data2) { }
}
private class volumeControl implements javax.microedition.media.control.VolumeControl
{
private int level = 100;
private boolean muted = false;
public int getLevel() { return level; }
public boolean isMuted() { return muted; }
public int setLevel(int value) { level = value; return level; }
public void setMute(boolean mute) { muted = mute; }
}
private class tempoControl implements javax.microedition.media.control.TempoControl
{
int tempo = 5000;
int rate = 5000;
public int getTempo() { return tempo; }
public int setTempo(int millitempo) { tempo = millitempo; return tempo; }
// RateControl interface
public int getMaxRate() { return rate; }
public int getMinRate() { return rate; }
public int getRate() { return rate; }
public int setRate(int millirate) { rate=millirate; return rate; }
}
}