📄 jmfplayer.java
字号:
package video;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
import javax.media.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
/**
* Demonstrate simple code to play a movie with Java Media Framework.
*
* @author Ian F. Darwin, http://www.darwinsys.com/
* @version $Id: JMFPlayer.java,v 1.9 2004/02/09 03:21:20 ian Exp $
*/
public class JMFPlayer extends JPanel implements ControllerListener {
JFrame parentFrame = null;
Container cp;
Component visualComponent = null;
Component controlComponent = null;
String mediaName;
Player thePlayer = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;
String str2="vfw:Microsoft WDM Image Capture (Win32):0";
private JButton capture=new JButton("capture");
private Buffer buf = null;
private BufferToImage btoi = null;
private ImageCapture imageCapture = null;
private Image img = null;
public JMFPlayer(JFrame pf) {
parentFrame = pf;
cp = this;
cp.setLayout(new BorderLayout());
capture.addActionListener(bcp);
cp.add(BorderLayout.NORTH,capture);
try {
di = CaptureDeviceManager.getDevice(str2);
System.out.println(di);
ml = di.getLocator();
thePlayer = Manager.createPlayer(ml);
thePlayer.addControllerListener(this);
} catch (MalformedURLException e) {
System.err.println("JMF URL creation error: " + e);
} catch (Exception e) {
System.err.println("JMF Player creation error: " + e);
return;
}
thePlayer.start(); // start playing
}
BCP bcp=new BCP();
class BCP implements ActionListener{
public void actionPerformed(ActionEvent e) {
FrameGrabbingControl fgc =(FrameGrabbingControl)thePlayer.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame(); // 获取当前祯并存入Buffer类
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf); // show the image
imageCapture=new ImageCapture(img);
}
}
public void stop() {
if (thePlayer == null)
return;
thePlayer.stop();
thePlayer.deallocate();
}
public void destroy() {
if (thePlayer == null)
return;
thePlayer.close();
}
public synchronized void controllerUpdate(ControllerEvent event) {
if (event instanceof RealizeCompleteEvent) {
if ((visualComponent = thePlayer.getVisualComponent()) != null)
cp.add(BorderLayout.CENTER, visualComponent);
if ((controlComponent = thePlayer.getControlPanelComponent()) != null)
cp.add(BorderLayout.SOUTH, controlComponent);
if (parentFrame != null) {
parentFrame.pack();
parentFrame.setTitle(mediaName);
}
}
}
public static void main(String[] argv) {
JFrame f = new JFrame("JMF Player Demo");
Container frameCP = f.getContentPane();
JMFPlayer p = new JMFPlayer(f);
frameCP.add(BorderLayout.CENTER, p);
f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -