📄 jmfplayer.java
字号:
if ( (player.getTargetState() != Controller.Started) && cbAutoPlay.getState() )
player.start();
}
else if (ce instanceof EndOfMediaEvent)
{
if (cbAutoLoop.getState())
{
player.setMediaTime(new Time(0));
/*
player.start();
player.stop();
sleep(1000);
player.start();
*/
player.prefetch();
}
}
else if (ce instanceof ControllerErrorEvent)
{
System.err.println("Received controller error");
killThePlayer();
if (visualComp != null)
visualComp.remove(zoomMenu);
panel.removeAll();
visualComp = null;
controlComp = null;
// setSize(320, 70);
setPlaceholder();
int nWidth,nHeight;
insets = getInsets();
nWidth = insets.left + insets.right + nImageWidth;
nHeight = insets.top + insets.bottom + nImageHeight;
setSize( nWidth, nHeight );
setVisible(true);
panel.setSize(nImageWidth, nImageHeight);
panel.setVisible(true);
player.removeControllerListener( this );
player.close();
player = null;
}
else if (ce instanceof SizeChangeEvent) {
if (panel != null) {
SizeChangeEvent sce = (SizeChangeEvent) ce;
int nooWidth = sce.getWidth();
int nooHeight = sce.getHeight();
if (controlComp != null)
nooHeight += controlComp.getPreferredSize().height;
if ( panel.getSize().width != nooWidth ||
panel.getSize().height != nooHeight) {
setSize(nooWidth + insets.left + insets.right,
nooHeight + insets.top + insets.bottom);
}
if (controlComp != null)
controlComp.invalidate();
}
}
else if (ce instanceof DeallocateEvent) {
if (visualComp != null) {
visualComp.remove(zoomMenu);
panel.remove(visualComp);
visualComp = null;
}
if (controlComp != null) {
panel.remove(controlComp);
controlComp = null;
}
player.removeControllerListener(this);
player.close();
player = null;
System.gc();
System.runFinalization();
panel.invalidate();
// setSize(320, 70);
setPlaceholder();
int nWidth,nHeight;
insets = getInsets();
nWidth = insets.left + insets.right + nImageWidth;
nHeight = insets.top + insets.bottom + nImageHeight;
setSize( nWidth, nHeight );
setVisible(true);
panel.setSize(nImageWidth, nImageHeight);
panel.setVisible(true);
sleep(500);
if (newPlayer != null) {
player = newPlayer;
newPlayer = null;
player.addControllerListener(this);
player.realize();
validate();
}
}
}
public void zoomTo(float z) {
insets = getInsets();
if (videoSizeControl != null) {
Dimension d = videoSizeControl.getInputVideoSize();
d.width = (int) (d.width * z);
d.height = (int) (d.height * z);
if (controlComp != null)
d.height += controlComp.getPreferredSize().height;
setSize(d.width + insets.left + insets.right,
d.height + insets.top + insets.bottom);
}
}
private void addPopupMenu(Component visual) {
MenuItem mi;
ActionListener zoomSelect;
zoomMenu = new PopupMenu("Zoom");
zoomSelect = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if (action.equals("1:2"))
zoomTo(0.5f);
else if (action.equals("1:1"))
zoomTo(1.0f);
else if (action.equals("2:1"))
zoomTo(2.0f);
else if (action.equals("4:1"))
zoomTo(4.0f);
}
};
visual.add(zoomMenu);
mi = new MenuItem("1:2");
zoomMenu.add(mi);
mi.addActionListener(zoomSelect);
mi = new MenuItem("1:1");
zoomMenu.add(mi);
mi.addActionListener(zoomSelect);
mi = new MenuItem("2:1");
zoomMenu.add(mi);
mi.addActionListener(zoomSelect);
mi = new MenuItem("4:1");
zoomMenu.add(mi);
mi.addActionListener(zoomSelect);
visual.addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent me) {
if (me.isPopupTrigger())
zoomMenu.show(visualComp, me.getX(), me.getY());
}
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger())
zoomMenu.show(visualComp, me.getX(), me.getY());
}
public void mouseClicked(MouseEvent me) {
if (me.isPopupTrigger())
zoomMenu.show(visualComp, me.getX(), me.getY());
}
} );
}
public void openFile(String filename) {
String mediaFile = filename;
Player player = null;
URL url = null;
try {
if ((url = new URL(mediaFile)) == null) {
Fatal("Can't build URL for " + mediaFile);
return;
}
try {
player = Manager.createPlayer(url);
} catch (NoPlayerException e) {
Fatal("Error: " + e);
JOptionPane.showMessageDialog(null,"不支持该格式!");
}
} catch (MalformedURLException e) {
Fatal("Error:" + e);
} catch (IOException e) {
Fatal("Error:" + e);
}
if (player != null) {
if (this.player == null) {
this.player = player;
player.addControllerListener(this);
player.realize();
this.filename = filename;
} else {
newPlayer = player;
this.filename = filename;
killThePlayer();
}
}
else{
JOptionPane.showMessageDialog(null,"无法播放该文件!");
}
}
public void setPlaceholder () {
////////////////////////////////////////////////////
//
imgBack=Toolkit.getDefaultToolkit().getImage(".\\img\\WMP03.jpg");
//
////////////////////////////////////////////////////
if ( areaImagePlaceholder == null ) {
if ( imgBack != null ) {
areaImagePlaceholder = new ImageArea ( imgBack );
areaImagePlaceholder.setInsets ( 0, 0, 0, 0 );
}
}
if ( areaImagePlaceholder != null ) {
panel.removeAll ();
panel.add ( areaImagePlaceholder, BorderLayout.CENTER );
}
}
public void splashwnd()
{
Image imgSplash=Toolkit.getDefaultToolkit().getImage(".\\img\\page.jpg");
ImageArea pnlImage=new ImageArea(imgSplash);
pnlImage.setInsets ( 0, 0, 0, 0 );
Window splashWindow = new Window(this);
splashWindow.add(pnlImage,BorderLayout.CENTER);
int nImageWidth = imgSplash.getWidth(this);
int nImageHeight = imgSplash.getHeight(this);
splashWindow.setSize(nImageWidth,nImageHeight);
splashWindow.setLocation(scmSize.width/2-nImageWidth/2,
scmSize.height/2-nImageHeight/2);
splashWindow.show();
splashWindow.toFront();
try{
Thread.currentThread().sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
splashWindow.dispose();
}
public static void main(String [] args) {
Player player = null;
String mediaFile = null;
if (args.length == 0) {
} else {
mediaFile = args[0];
// URL for our media file
URL url = null;
try {
// Create an url from the file name and the url to the
// document containing this applet.
if ((url = new URL(mediaFile)) == null)
Fatal("Error: Can't build URL for " + mediaFile);
// Create an instance of a player for this media
try {
player = Manager.createPlayer(url);
} catch (NoPlayerException e) {
Fatal("Error: Could not create player for " + url);
}
} catch (MalformedURLException e) {
Fatal("Error: Invalid media file URL!");
} catch (IOException e) {
Fatal("Error: IO exception creating player for " + url);
}
}
if (player == null)
mediaFile = null;
JMFPlayer frmJmfplayer = new JMFPlayer(player, mediaFile);
frmJmfplayer.pack();
frmJmfplayer.show();
}
static void Fatal(String s) {
// MessageBox mb = new MessageBox("JMF Error", s);
System.out.println (s);
}
}
class ImageArea extends Panel {
private Image image = null;
private Insets insetsBorder = new Insets (6,6,6,6);
public ImageArea ( Image image ) {
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,0);
try
{
mt.waitForID(0);
}
catch(Exception e)
{
e.printStackTrace();
}
this.image = image;
}
public void setImage ( Image image ) {
this.image = image;
}
public void setInsets ( int nLeft, int nTop, int nRight, int nBottom ) {
insetsBorder.left = nLeft;
insetsBorder.top = nTop;
insetsBorder.right = nRight;
insetsBorder.bottom = nBottom;
}
public void paint ( Graphics g ) {
Dimension dim;
dim = this.getSize ();
dim.width -= insetsBorder.left + insetsBorder.right;
dim.height -= insetsBorder.top + insetsBorder.bottom;
g.drawImage ( image, insetsBorder.left, insetsBorder.top, dim.width, dim.height, this );
}
public Dimension getPreferredSize () {
Dimension dim;
if ( image == null )
dim = new Dimension ( 0, 0 );
else
dim = new Dimension ( image.getWidth(this), image.getHeight(this) );
dim.width += insetsBorder.left + insetsBorder.right;
dim.height += insetsBorder.top + insetsBorder.bottom;
return ( dim );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -