KazaamMediaPlayer.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.viewer.jmfplayer |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap
*
* A peer-to-peer and file sharing client.
*
* Copyright Kazaam 2002-2003.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* Modified by Steffen Pingel for XNap 3.
*/
package org.xnap.plugin.viewer.jmfplayer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.CachingControl;
import javax.media.CachingControlEvent;
import javax.media.Controller;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerErrorEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* KazaamMediaPlayer.java - Kazaam copyright 2002 - all rights reserved.
* 12/29/2002
* @author joker10000y
*/
public class KazaamMediaPlayer extends JPanel implements ControllerListener {
// media Player
private static Player player = null;
// component in which video is playing
Component visualComponent = null;
// controls gain, position, start, stop
Component controlComponent = null;
// displays progress during download
Component progressBar = null;
boolean firstTime = true;
long CachingSize = 0L;
int controlPanelHeight = 0;
int videoWidth = 0;
int videoHeight = 0;
private static boolean loopPlayBack = false;
private static boolean isStarted = false;
private Dimension dimFrameSizeBeforeFullScreen = null;
private Window windowFullScreen = null;
private MouseListener listenerMouseFullScreen;
private Window theParentWindow = null;
private Frame theParentFrame = null;
/** parentIsFrameOrWindow - 0=Frame, 1=Window */
private int parentIsFrameOrWindow = 0;
public static boolean isStarted()
{
if(player!=null)
{
return true;
}
else
{
return false;
}
}
/**
* Read the applet file parameter and create the media
* player.
*/
public void setPlayFile(File theMediaFile)
{
//$ System.out.println("Applet.init() is called");
setLayout(new java.awt.BorderLayout());
setBackground(Color.black);
setBounds(0, 0, 320, 240);
setDoubleBuffered(true);
// URL for our media file
MediaLocator mrl = null;
URL url = null;
// Get the media filename info.
// The applet tag should contain the path to the
// source media file, relative to the html page.
if ((theMediaFile == null))
{
Fatal("Invalid media file parameter");
}
else
{
try
{
url = theMediaFile.toURL();
//mediaFile = url.toExternalForm();
}
catch (MalformedURLException mue) {System.out.println(mue);}
try
{
// Create a media locator from the file name
if ((mrl = new MediaLocator(url)) == null)
{
Fatal("Can't build URL for " + url.toString());
}
// Create an instance of a player for this media
try
{
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
player = Manager.createPlayer(mrl);
}
catch (NoPlayerException e)
{
System.out.println(e);
Fatal("Could not create player for " + mrl);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(this);
}
catch (MalformedURLException e)
{
Fatal("Invalid media file URL!");
}
catch (IOException e)
{
Fatal("IO exception creating player for " + mrl);
}
// This Component assumes that its start() calls
// player.start(). This causes the player to become
// realized. Once realized, the Component will get
// the visual and control panel components and add
// them to the Component. These components are not added
// during init() because they are long operations that
// would make us appear unresposive to the user.
start();
}
}
/** setLoopPlayback(boolean loopOnPlayBack) */
public static void setLoopPlayback(boolean loopOnPlayBack)
{
loopPlayBack=loopOnPlayBack;
}
/** getLoopPlayback() - returns true or false setting of loop on playback. */
public static boolean getLoopPlayback()
{
return loopPlayBack;
}
/**
* Start media file playback. This function is called the
* first time that the Applet runs and every
* time the user re-enters the page.
*/
public void start()
{
//$ System.out.println("Applet.start() is called");
// Call start() to prefetch and start the player.
if (player != null)
{
try
{
player.start();
}
catch(Exception e)
{
}
}
}
/**
* Pause media file playback and release resource before
* leaving the page.
*/
public void pause()
{
//$ System.out.println("stop() is called");
if (player != null)
{
try
{
player.stop();
player.deallocate();
}
catch(Exception e)
{
}
}
}
/** stop() - Stop the Media Preview Player */
public void stop()
{
//$ System.out.println("Applet.destroy() is called");
try
{
player.stop();
player.deallocate();
}
catch(Exception e)
{
}
try
{
player.close();
}
catch(Exception e)
{
}
// media Player
player = null;
// component in which video is playing
visualComponent = null;
// controls gain, position, start, stop
controlComponent = null;
// displays progress during download
progressBar = null;
firstTime = true;
CachingSize = 0L;
controlPanelHeight = 0;
videoWidth = 0;
videoHeight = 0;
System.gc();
}
/** setMediaTimeInSeconds(int secondsTime) - set the current play location by time (in seconds) */
public void setMediaTimeInSeconds(int secondsTime)
{
if (player != null)
{
player.setMediaTime(new Time(secondsTime));
}
}
/** getMediaTimeInSeconds() - return the current Media Play Time (in seconds) */
public double getMediaTimeInSeconds()
{
if (player != null)
{
return player.getMediaTime().getSeconds();
}
return -1;
}
/** setMediaTimeInNanoSeconds(long nanosecondsTime) - set the current play location by time (in nanoseconds) */
public void setMediaTimeInNanoSeconds(long nanosecondsTime)
{
if (player != null)
{
player.setMediaTime(new Time(nanosecondsTime));
}
}
/** getMediaTimeInNanoSeconds() - return the current Media Play Time (in nanoseconds) */
public long getMediaTimeInNanoSeconds()
{
if (player != null)
{
return player.getMediaTime().getNanoseconds();
}
return -1;
}
/**
* This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event
*/
public synchronized void controllerUpdate(ControllerEvent event)
{
// If we're getting messages from a dead player,
// just leave
if (player != null)
{}
else
{
return;
}
// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent)
{
if (progressBar != null)
{
remove(progressBar);
progressBar = null;
}
int width = 320;
int height = 0;
if (controlComponent != null){}
else
{
if (( controlComponent = player.getControlPanelComponent()) != null)
{
controlPanelHeight = controlComponent.getPreferredSize().height;
add(controlComponent, BorderLayout.SOUTH);
height += controlPanelHeight;
}
}
if (visualComponent != null){}
else
{
if (( visualComponent = player.getVisualComponent())!= null)
{
add(visualComponent,BorderLayout.CENTER);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
visualComponent.setBackground(Color.black);
}
}
setBounds(0, 0, width, height);
if (controlComponent != null)
{
controlComponent.setBounds(0, videoHeight,
width, controlPanelHeight);
controlComponent.invalidate();
}
}
else if (event instanceof CachingControlEvent)
{
if (player.getState() > Controller.Realizing)
{
return;
}
// Put a progress bar up when downloading starts,
// take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();
// Add the bar if not already there ...
if (progressBar != null){}
else
{
if ((progressBar = cc.getControlComponent()) != null)
{
add(progressBar, BorderLayout.SOUTH);
setSize(progressBar.getPreferredSize());
validate();
}
}
}
else if (event instanceof EndOfMediaEvent)
{
// We've reached the end of the media; rewind and
// start over
if(getLoopPlayback()==true)
{
player.setMediaTime(new Time(0));
player.start();
}
else
{
player.setMediaTime(new Time(0));
//stop();
// media Player
//player = null;
// component in which video is playing
//visualComponent = null;
// controls gain, position, start, stop
//controlComponent = null;
// displays progress during download
//progressBar = null;
//firstTime = true;
//CachingSize = 0L;
//controlPanelHeight = 0;
//videoWidth = 0;
//videoHeight = 0;
}
}
else if (event instanceof ControllerErrorEvent)
{
// Tell TypicalPlayerApplet.start() to call it a day
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
}
else if (event instanceof ControllerClosedEvent)
{
removeAll();
}
}
protected void setFullScreen ( boolean boolFullScreen, Window parentWindow )
{
parentIsFrameOrWindow=1;
Dimension dimScreen;
Dimension dimPrefSize;
Rectangle rectVideo;
if ( visualComponent == null )
{
return;
}
if ( boolFullScreen == true && visualComponent.getParent() != windowFullScreen )
{
dimFrameSizeBeforeFullScreen = this.getSize ();
dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
if ( windowFullScreen == null )
{
windowFullScreen = new Window ( parentWindow );
windowFullScreen.setLayout( null );
windowFullScreen.setBackground ( Color.black );
}
windowFullScreen.setBounds ( 0, 0, dimScreen.width, dimScreen.height );
remove ( visualComponent );
dimPrefSize = visualComponent.getPreferredSize ();
if ( controlComponent != null)
{
remove ( controlComponent );
}
rectVideo = new Rectangle ( 0, 0, dimScreen.width, dimScreen.height );
if ( (float)dimPrefSize.width/dimPrefSize.height >= (float)dimScreen.width/dimScreen.height )
{
rectVideo.height = (dimPrefSize.height * dimScreen.width) / dimPrefSize.width;
rectVideo.y = (dimScreen.height - rectVideo.height) / 2;
}
else
{
rectVideo.width = (dimPrefSize.width * dimScreen.height) / dimPrefSize.height;
rectVideo.x = (dimScreen.width - rectVideo.width) / 2;
}
Toolkit.getDefaultToolkit().sync();
windowFullScreen.add ( visualComponent );
windowFullScreen.setVisible ( true );
visualComponent.setBounds ( rectVideo );
windowFullScreen.validate ();
listenerMouseFullScreen = new MouseAdapter ()
{
public void mouseClicked ( MouseEvent event )
{
if(parentIsFrameOrWindow == 0)
{
setFullScreen( false, theParentFrame );
}
else if(parentIsFrameOrWindow == 1)
{
setFullScreen( false, theParentWindow );
}
}
};
visualComponent.addMouseListener ( listenerMouseFullScreen );
}
else if ( boolFullScreen == false && visualComponent.getParent() == windowFullScreen )
{
this.setVisible ( false );
visualComponent.removeMouseListener ( listenerMouseFullScreen );
Toolkit.getDefaultToolkit().sync();
windowFullScreen.setVisible ( false );
windowFullScreen.remove ( visualComponent );
add ( visualComponent, BorderLayout.CENTER );
if ( controlComponent != null)
{
add ( controlComponent, BorderLayout.SOUTH );
}
if ( dimFrameSizeBeforeFullScreen != null )
{
this.setSize ( dimFrameSizeBeforeFullScreen );
this.validate ();
}
this.setVisible ( true );
}
}
protected void setFullScreen ( boolean boolFullScreen, Frame parentFrame )
{
parentIsFrameOrWindow=0;
Dimension dimScreen;
Dimension dimPrefSize;
Rectangle rectVideo;
if ( visualComponent == null )
{
return;
}
if ( boolFullScreen == true && visualComponent.getParent() != windowFullScreen )
{
dimFrameSizeBeforeFullScreen = this.getSize ();
dimScreen = Toolkit.getDefaultToolkit().getScreenSize();
if ( windowFullScreen == null )
{
windowFullScreen = new Window ( parentFrame );
windowFullScreen.setLayout( null );
windowFullScreen.setBackground ( Color.black );
}
windowFullScreen.setBounds ( 0, 0, dimScreen.width, dimScreen.height );
remove ( visualComponent );
dimPrefSize = visualComponent.getPreferredSize ();
if ( controlComponent != null)
{
remove ( controlComponent );
}
rectVideo = new Rectangle ( 0, 0, dimScreen.width, dimScreen.height );
if ( (float)dimPrefSize.width/dimPrefSize.height >= (float)dimScreen.width/dimScreen.height )
{
rectVideo.height = (dimPrefSize.height * dimScreen.width) / dimPrefSize.width;
rectVideo.y = (dimScreen.height - rectVideo.height) / 2;
}
else
{
rectVideo.width = (dimPrefSize.width * dimScreen.height) / dimPrefSize.height;
rectVideo.x = (dimScreen.width - rectVideo.width) / 2;
}
Toolkit.getDefaultToolkit().sync();
windowFullScreen.add ( visualComponent );
windowFullScreen.setVisible ( true );
visualComponent.setBounds ( rectVideo );
windowFullScreen.validate ();
listenerMouseFullScreen = new MouseAdapter ()
{
public void mouseClicked ( MouseEvent event )
{
if(parentIsFrameOrWindow == 0)
{
setFullScreen( false, theParentFrame );
}
else if(parentIsFrameOrWindow == 1)
{
setFullScreen( false, theParentWindow );
}
}
};
visualComponent.addMouseListener ( listenerMouseFullScreen );
}
else if ( boolFullScreen == false && visualComponent.getParent() == windowFullScreen )
{
this.setVisible ( false );
visualComponent.removeMouseListener ( listenerMouseFullScreen );
Toolkit.getDefaultToolkit().sync();
windowFullScreen.setVisible ( false );
windowFullScreen.remove ( visualComponent );
add ( visualComponent, BorderLayout.CENTER );
if ( controlComponent != null)
{
add ( controlComponent, BorderLayout.SOUTH );
}
if ( dimFrameSizeBeforeFullScreen != null )
{
this.setSize ( dimFrameSizeBeforeFullScreen );
this.validate ();
}
this.setVisible ( true );
}
}
void Fatal (String s)
{
// Applications will make various choices about what
// to do here. We print a message
System.err.println("FATAL ERROR: " + s);
throw new Error(s); // Invoke the uncaught exception
// handler System.exit() is another
// choice.
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
KazaamMediaPlayer sp = new KazaamMediaPlayer();
frame.getContentPane().add(sp);
frame.setSize(400,400);
frame.setVisible(true);
//sp.setPlayFile(new File("C:\\Documents and Settings\\Adam\\.kazaam\\My Media\\Video\\aaa.mpeg"));
}
}
The table below shows all metrics for KazaamMediaPlayer.java.




