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 package org.xnap.plugin.viewer.jmfplayer;
27
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Component;
31 import java.awt.Dimension;
32 import java.awt.Frame;
33 import java.awt.Rectangle;
34 import java.awt.Toolkit;
35 import java.awt.Window;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseListener;
39 import java.io.File;
40 import java.io.IOException;
41 import java.net.MalformedURLException;
42 import java.net.URL;
43
44 import javax.media.CachingControl;
45 import javax.media.CachingControlEvent;
46 import javax.media.Controller;
47 import javax.media.ControllerClosedEvent;
48 import javax.media.ControllerErrorEvent;
49 import javax.media.ControllerEvent;
50 import javax.media.ControllerListener;
51 import javax.media.EndOfMediaEvent;
52 import javax.media.Manager;
53 import javax.media.MediaLocator;
54 import javax.media.NoPlayerException;
55 import javax.media.Player;
56 import javax.media.RealizeCompleteEvent;
57 import javax.media.Time;
58 import javax.swing.JFrame;
59 import javax.swing.JPanel;
60
61 /***
62 * KazaamMediaPlayer.java - Kazaam copyright 2002 - all rights reserved.
63 * 12/29/2002
64 * @author joker10000y
65 */
66 public class KazaamMediaPlayer extends JPanel implements ControllerListener {
67
68
69
70 private static Player player = null;
71
72 Component visualComponent = null;
73
74 Component controlComponent = null;
75
76 Component progressBar = null;
77 boolean firstTime = true;
78 long CachingSize = 0L;
79 int controlPanelHeight = 0;
80 int videoWidth = 0;
81 int videoHeight = 0;
82 private static boolean loopPlayBack = false;
83 private static boolean isStarted = false;
84 private Dimension dimFrameSizeBeforeFullScreen = null;
85 private Window windowFullScreen = null;
86 private MouseListener listenerMouseFullScreen;
87 private Window theParentWindow = null;
88 private Frame theParentFrame = null;
89 /*** parentIsFrameOrWindow - 0=Frame, 1=Window */
90 private int parentIsFrameOrWindow = 0;
91
92 public static boolean isStarted()
93 {
94 if(player!=null)
95 {
96 return true;
97 }
98 else
99 {
100 return false;
101 }
102 }
103
104 /***
105 * Read the applet file parameter and create the media
106 * player.
107 */
108 public void setPlayFile(File theMediaFile)
109 {
110
111 setLayout(new java.awt.BorderLayout());
112 setBackground(Color.black);
113 setBounds(0, 0, 320, 240);
114 setDoubleBuffered(true);
115
116 MediaLocator mrl = null;
117 URL url = null;
118
119
120
121
122
123 if ((theMediaFile == null))
124 {
125 Fatal("Invalid media file parameter");
126 }
127 else
128 {
129 try
130 {
131 url = theMediaFile.toURL();
132
133 }
134 catch (MalformedURLException mue) {System.out.println(mue);}
135
136 try
137 {
138
139 if ((mrl = new MediaLocator(url)) == null)
140 {
141 Fatal("Can't build URL for " + url.toString());
142 }
143
144
145 try
146 {
147 Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
148 player = Manager.createPlayer(mrl);
149 }
150 catch (NoPlayerException e)
151 {
152 System.out.println(e);
153 Fatal("Could not create player for " + mrl);
154 }
155
156
157 player.addControllerListener(this);
158
159 }
160 catch (MalformedURLException e)
161 {
162 Fatal("Invalid media file URL!");
163 }
164 catch (IOException e)
165 {
166 Fatal("IO exception creating player for " + mrl);
167 }
168
169
170
171
172
173
174
175
176 start();
177 }
178 }
179
180
181 /*** setLoopPlayback(boolean loopOnPlayBack) */
182 public static void setLoopPlayback(boolean loopOnPlayBack)
183 {
184 loopPlayBack=loopOnPlayBack;
185 }
186
187 /*** getLoopPlayback() - returns true or false setting of loop on playback. */
188 public static boolean getLoopPlayback()
189 {
190 return loopPlayBack;
191 }
192
193 /***
194 * Start media file playback. This function is called the
195 * first time that the Applet runs and every
196 * time the user re-enters the page.
197 */
198
199 public void start()
200 {
201
202
203 if (player != null)
204 {
205 try
206 {
207 player.start();
208 }
209 catch(Exception e)
210 {
211
212 }
213 }
214 }
215
216 /***
217 * Pause media file playback and release resource before
218 * leaving the page.
219 */
220 public void pause()
221 {
222
223 if (player != null)
224 {
225 try
226 {
227 player.stop();
228 player.deallocate();
229 }
230 catch(Exception e)
231 {
232
233 }
234 }
235 }
236
237 /*** stop() - Stop the Media Preview Player */
238 public void stop()
239 {
240
241 try
242 {
243 player.stop();
244 player.deallocate();
245 }
246 catch(Exception e)
247 {
248
249 }
250 try
251 {
252 player.close();
253 }
254 catch(Exception e)
255 {
256
257 }
258
259 player = null;
260
261 visualComponent = null;
262
263 controlComponent = null;
264
265 progressBar = null;
266 firstTime = true;
267 CachingSize = 0L;
268 controlPanelHeight = 0;
269 videoWidth = 0;
270 videoHeight = 0;
271 System.gc();
272 }
273
274 /*** setMediaTimeInSeconds(int secondsTime) - set the current play location by time (in seconds) */
275 public void setMediaTimeInSeconds(int secondsTime)
276 {
277 if (player != null)
278 {
279 player.setMediaTime(new Time(secondsTime));
280 }
281 }
282
283 /*** getMediaTimeInSeconds() - return the current Media Play Time (in seconds) */
284 public double getMediaTimeInSeconds()
285 {
286 if (player != null)
287 {
288 return player.getMediaTime().getSeconds();
289 }
290 return -1;
291 }
292 /*** setMediaTimeInNanoSeconds(long nanosecondsTime) - set the current play location by time (in nanoseconds) */
293 public void setMediaTimeInNanoSeconds(long nanosecondsTime)
294 {
295 if (player != null)
296 {
297 player.setMediaTime(new Time(nanosecondsTime));
298 }
299 }
300
301 /*** getMediaTimeInNanoSeconds() - return the current Media Play Time (in nanoseconds) */
302 public long getMediaTimeInNanoSeconds()
303 {
304 if (player != null)
305 {
306 return player.getMediaTime().getNanoseconds();
307 }
308 return -1;
309 }
310
311 /***
312 * This controllerUpdate function must be defined in order to
313 * implement a ControllerListener interface. This
314 * function will be called whenever there is a media event
315 */
316 public synchronized void controllerUpdate(ControllerEvent event)
317 {
318
319
320 if (player != null)
321 {}
322 else
323 {
324 return;
325 }
326
327
328 if (event instanceof RealizeCompleteEvent)
329 {
330 if (progressBar != null)
331 {
332 remove(progressBar);
333 progressBar = null;
334 }
335
336 int width = 320;
337 int height = 0;
338 if (controlComponent != null){}
339 else
340 {
341 if (( controlComponent = player.getControlPanelComponent()) != null)
342 {
343
344 controlPanelHeight = controlComponent.getPreferredSize().height;
345 add(controlComponent, BorderLayout.SOUTH);
346 height += controlPanelHeight;
347 }
348 }
349 if (visualComponent != null){}
350 else
351 {
352 if (( visualComponent = player.getVisualComponent())!= null)
353 {
354 add(visualComponent,BorderLayout.CENTER);
355 Dimension videoSize = visualComponent.getPreferredSize();
356 videoWidth = videoSize.width;
357 videoHeight = videoSize.height;
358 width = videoWidth;
359 height += videoHeight;
360 visualComponent.setBounds(0, 0, videoWidth, videoHeight);
361 visualComponent.setBackground(Color.black);
362 }
363 }
364 setBounds(0, 0, width, height);
365 if (controlComponent != null)
366 {
367 controlComponent.setBounds(0, videoHeight,
368 width, controlPanelHeight);
369 controlComponent.invalidate();
370 }
371
372 }
373 else if (event instanceof CachingControlEvent)
374 {
375 if (player.getState() > Controller.Realizing)
376 {
377 return;
378 }
379
380
381
382 CachingControlEvent e = (CachingControlEvent) event;
383 CachingControl cc = e.getCachingControl();
384
385
386 if (progressBar != null){}
387 else
388 {
389 if ((progressBar = cc.getControlComponent()) != null)
390 {
391 add(progressBar, BorderLayout.SOUTH);
392 setSize(progressBar.getPreferredSize());
393 validate();
394 }
395 }
396 }
397 else if (event instanceof EndOfMediaEvent)
398 {
399
400
401
402 if(getLoopPlayback()==true)
403 {
404 player.setMediaTime(new Time(0));
405 player.start();
406 }
407 else
408 {
409 player.setMediaTime(new Time(0));
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424 }
425 }
426 else if (event instanceof ControllerErrorEvent)
427 {
428
429 player = null;
430 Fatal(((ControllerErrorEvent)event).getMessage());
431 }
432 else if (event instanceof ControllerClosedEvent)
433 {
434 removeAll();
435 }
436 }
437
438 protected void setFullScreen ( boolean boolFullScreen, Window parentWindow )
439 {
440 parentIsFrameOrWindow=1;
441 Dimension dimScreen;
442 Dimension dimPrefSize;
443 Rectangle rectVideo;
444
445
446 if ( visualComponent == null )
447 {
448 return;
449 }
450
451 if ( boolFullScreen == true && visualComponent.getParent() != windowFullScreen )
452 {
453 dimFrameSizeBeforeFullScreen = this.getSize ();
454
455 dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
456 if ( windowFullScreen == null )
457 {
458 windowFullScreen = new Window ( parentWindow );
459 windowFullScreen.setLayout( null );
460 windowFullScreen.setBackground ( Color.black );
461 }
462 windowFullScreen.setBounds ( 0, 0, dimScreen.width, dimScreen.height );
463 remove ( visualComponent );
464 dimPrefSize = visualComponent.getPreferredSize ();
465 if ( controlComponent != null)
466 {
467 remove ( controlComponent );
468 }
469
470 rectVideo = new Rectangle ( 0, 0, dimScreen.width, dimScreen.height );
471 if ( (float)dimPrefSize.width/dimPrefSize.height >= (float)dimScreen.width/dimScreen.height )
472 {
473 rectVideo.height = (dimPrefSize.height * dimScreen.width) / dimPrefSize.width;
474 rectVideo.y = (dimScreen.height - rectVideo.height) / 2;
475 }
476 else
477 {
478 rectVideo.width = (dimPrefSize.width * dimScreen.height) / dimPrefSize.height;
479 rectVideo.x = (dimScreen.width - rectVideo.width) / 2;
480 }
481
482 Toolkit.getDefaultToolkit().sync();
483 windowFullScreen.add ( visualComponent );
484 windowFullScreen.setVisible ( true );
485 visualComponent.setBounds ( rectVideo );
486 windowFullScreen.validate ();
487
488 listenerMouseFullScreen = new MouseAdapter ()
489 {
490 public void mouseClicked ( MouseEvent event )
491 {
492 if(parentIsFrameOrWindow == 0)
493 {
494 setFullScreen( false, theParentFrame );
495 }
496 else if(parentIsFrameOrWindow == 1)
497 {
498 setFullScreen( false, theParentWindow );
499 }
500 }
501 };
502 visualComponent.addMouseListener ( listenerMouseFullScreen );
503 }
504 else if ( boolFullScreen == false && visualComponent.getParent() == windowFullScreen )
505 {
506 this.setVisible ( false );
507 visualComponent.removeMouseListener ( listenerMouseFullScreen );
508 Toolkit.getDefaultToolkit().sync();
509 windowFullScreen.setVisible ( false );
510 windowFullScreen.remove ( visualComponent );
511 add ( visualComponent, BorderLayout.CENTER );
512 if ( controlComponent != null)
513 {
514 add ( controlComponent, BorderLayout.SOUTH );
515 }
516 if ( dimFrameSizeBeforeFullScreen != null )
517 {
518 this.setSize ( dimFrameSizeBeforeFullScreen );
519 this.validate ();
520 }
521 this.setVisible ( true );
522 }
523 }
524
525 protected void setFullScreen ( boolean boolFullScreen, Frame parentFrame )
526 {
527 parentIsFrameOrWindow=0;
528 Dimension dimScreen;
529 Dimension dimPrefSize;
530 Rectangle rectVideo;
531
532
533 if ( visualComponent == null )
534 {
535 return;
536 }
537
538 if ( boolFullScreen == true && visualComponent.getParent() != windowFullScreen )
539 {
540 dimFrameSizeBeforeFullScreen = this.getSize ();
541
542 dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
543 if ( windowFullScreen == null )
544 {
545 windowFullScreen = new Window ( parentFrame );
546 windowFullScreen.setLayout( null );
547 windowFullScreen.setBackground ( Color.black );
548 }
549 windowFullScreen.setBounds ( 0, 0, dimScreen.width, dimScreen.height );
550 remove ( visualComponent );
551 dimPrefSize = visualComponent.getPreferredSize ();
552 if ( controlComponent != null)
553 {
554 remove ( controlComponent );
555 }
556
557 rectVideo = new Rectangle ( 0, 0, dimScreen.width, dimScreen.height );
558 if ( (float)dimPrefSize.width/dimPrefSize.height >= (float)dimScreen.width/dimScreen.height )
559 {
560 rectVideo.height = (dimPrefSize.height * dimScreen.width) / dimPrefSize.width;
561 rectVideo.y = (dimScreen.height - rectVideo.height) / 2;
562 }
563 else
564 {
565 rectVideo.width = (dimPrefSize.width * dimScreen.height) / dimPrefSize.height;
566 rectVideo.x = (dimScreen.width - rectVideo.width) / 2;
567 }
568
569 Toolkit.getDefaultToolkit().sync();
570 windowFullScreen.add ( visualComponent );
571 windowFullScreen.setVisible ( true );
572 visualComponent.setBounds ( rectVideo );
573 windowFullScreen.validate ();
574
575 listenerMouseFullScreen = new MouseAdapter ()
576 {
577 public void mouseClicked ( MouseEvent event )
578 {
579 if(parentIsFrameOrWindow == 0)
580 {
581 setFullScreen( false, theParentFrame );
582 }
583 else if(parentIsFrameOrWindow == 1)
584 {
585 setFullScreen( false, theParentWindow );
586 }
587 }
588 };
589 visualComponent.addMouseListener ( listenerMouseFullScreen );
590 }
591 else if ( boolFullScreen == false && visualComponent.getParent() == windowFullScreen )
592 {
593 this.setVisible ( false );
594 visualComponent.removeMouseListener ( listenerMouseFullScreen );
595 Toolkit.getDefaultToolkit().sync();
596 windowFullScreen.setVisible ( false );
597 windowFullScreen.remove ( visualComponent );
598 add ( visualComponent, BorderLayout.CENTER );
599 if ( controlComponent != null)
600 {
601 add ( controlComponent, BorderLayout.SOUTH );
602 }
603 if ( dimFrameSizeBeforeFullScreen != null )
604 {
605 this.setSize ( dimFrameSizeBeforeFullScreen );
606 this.validate ();
607 }
608 this.setVisible ( true );
609 }
610 }
611
612
613 void Fatal (String s)
614 {
615
616
617 System.err.println("FATAL ERROR: " + s);
618 throw new Error(s);
619
620
621
622 }
623 public static void main(String[] args)
624 {
625 JFrame frame = new JFrame();
626 frame.getContentPane().setLayout(null);
627
628 KazaamMediaPlayer sp = new KazaamMediaPlayer();
629
630 frame.getContentPane().add(sp);
631 frame.setSize(400,400);
632 frame.setVisible(true);
633
634 }
635 }