View Javadoc

1   /*
2    *  XNap
3    *
4    *  A peer-to-peer and file sharing client.
5    *
6    *  Copyright Kazaam 2002-2003. 
7    *
8    *  This program is free software; you can redistribute it and/or modify
9    *  it under the terms of the GNU General Public License as published by
10   *  the Free Software Foundation; either version 2 of the License, or
11   *  (at your option) any later version.
12   *
13   *  This program is distributed in the hope that it will be useful,
14   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   *  GNU General Public License for more details.
17   *
18   *  You should have received a copy of the GNU General Public License
19   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   *
22   */
23  /*
24   * Modified by Steffen Pingel for XNap 3.
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      // media Player
69  
70      private static Player player = null;
71      // component in which video is playing
72      Component visualComponent = null;
73      // controls gain, position, start, stop
74      Component controlComponent = null;
75      // displays progress during download
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 	//$ System.out.println("Applet.init() is called");
111 	setLayout(new java.awt.BorderLayout());
112 	setBackground(Color.black);
113 	setBounds(0, 0, 320, 240);
114 	setDoubleBuffered(true);
115 	// URL for our media file
116 	MediaLocator mrl = null;
117 	URL url = null;
118 
119 	// Get the media filename info.
120 	// The applet tag should contain the path to the
121  	// source media file, relative to the html page.
122 	
123 	if ((theMediaFile == null))
124         {
125 	    Fatal("Invalid media file parameter");
126         }
127         else
128         {
129             try 
130             {
131                 url = theMediaFile.toURL();
132                 //mediaFile = url.toExternalForm();
133             } 
134             catch (MalformedURLException mue) {System.out.println(mue);}
135 
136             try 
137             {
138                 // Create a media locator from the file name
139                 if ((mrl = new MediaLocator(url)) == null)
140                 {
141                     Fatal("Can't build URL for " + url.toString());
142                 }
143 
144                 // Create an instance of a player for this media
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                 // Add ourselves as a listener for a player's events
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             // This Component assumes that its start() calls 
170             // player.start(). This causes the player to become
171             // realized. Once realized, the Component will get
172             // the visual and control panel components and add
173             // them to the Component. These components are not added
174             // during init() because they are long operations that
175             // would make us appear unresposive to the user.
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 	//$ System.out.println("Applet.start() is called");
202         // Call start() to prefetch and start the player.
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 	//$ System.out.println("stop() is called");
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 	//$ System.out.println("Applet.destroy() is called");
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     	// media Player
259     	player = null;
260     	// component in which video is playing
261     	visualComponent = null;
262     	// controls gain, position, start, stop
263     	controlComponent = null;
264     	// displays progress during download
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 	// If we're getting messages from a dead player, 
319 	// just leave
320         if (player != null)
321         {}
322         else
323         {
324             return;
325         }
326 	// When the player is Realized, get the visual 
327 	// and control components and add them to the Applet
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             // Put a progress bar up when downloading starts, 
381 	    // take it down when downloading ends.
382 	    CachingControlEvent e = (CachingControlEvent) event;
383 	    CachingControl cc = e.getCachingControl();
384 
385 	    // Add the bar if not already there ...
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 	    // We've reached the end of the media; rewind and
400 	    // start over
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                 //stop();
411     	// media Player
412     	//player = null;
413     	// component in which video is playing
414     	//visualComponent = null;
415     	// controls gain, position, start, stop
416     	//controlComponent = null;
417     	// displays progress during download
418     	//progressBar = null;
419     	//firstTime = true;
420     	//CachingSize = 0L;    
421     	//controlPanelHeight = 0;
422     	//videoWidth = 0;
423     	//videoHeight = 0;
424             }
425 	} 
426         else if (event instanceof ControllerErrorEvent) 
427         {
428 	    // Tell TypicalPlayerApplet.start() to call it a day
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 	// Applications will make various choices about what
616 	// to do here. We print a message
617 	System.err.println("FATAL ERROR: " + s);
618 	throw new Error(s); // Invoke the uncaught exception
619 			    // handler System.exit() is another
620 			    // choice.
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         //sp.setPlayFile(new File("C://Documents and Settings//Adam//.kazaam//My Media//Video//aaa.mpeg"));
634     }
635 }