?? seek.java
字號(hào):
package bin;
//To seek to individual frames inside a movie(定位到影片中的幀)
import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import java.io.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.control.FramePositioningControl;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
public class Seek implements ControllerListener{
//播放器接口
Player p;
//幀定位控制接口
FramePositioningControl fpc;
//獲取幀控制接口
FrameGrabbingControl fgc ;
//convert a video Buffer object to an AWT Image object
BufferToImage bti;
Object waitSync = new Object();
boolean stateTransitionOK = true;
//影片總幀數(shù)
int totalFrames = 0;
//the name of bmp file saved
int bmpName=1;
int time=0;
//影片格式
VideoFormat vf = null;
//利用管理器創(chuàng)建一個(gè)數(shù)據(jù)源
Seek(String args) {
//the location of media content
MediaLocator ml;
//找不到影片
if ((ml = new MediaLocator(args)) == null) {
System.err.println("Cannot build media locator from: " + args);
System.err.println("file path error!");
System.exit(0);
}
//數(shù)據(jù)源
DataSource ds = null;
// Create a DataSource use given the media locator.
try {
ds = Manager.createDataSource(ml);
totalFrames = FramePositioningControl.FRAME_UNKNOWN;
} catch (Exception e) {
System.err.println("1Cannot create DataSource from: " + ml);
}
if (!open(ds))
System.err.println("2Cannot create DataSource from: " + ml);
}
/**
* Given a DataSource, create a player and use that player
* as a player to playback the media.
*/
//創(chuàng)建播放器并且播放
public boolean open(DataSource ds) {
System.err.println("create player for: " + ds.getContentType());
//創(chuàng)建播放器
try {
p = Manager.createPlayer(ds);
} catch (Exception e) {
System.err.println("Failed to create a player from the given DataSource: " + e);
return false;
}
p.addControllerListener(this);
//實(shí)例化播放器
p.realize();
if (!waitForState(p.Realized)) {
System.err.println("Failed to realize the player.");
return false;
}
// Try to retrieve a FramePositioningControl from the player.
fpc = (FramePositioningControl)p.getControl("javax.media.control.FramePositioningControl");
fgc = (FrameGrabbingControl)p.getControl("javax.media.control.FrameGrabbingControl");
if (fpc == null) {
System.err.println("The player does not support FramePositioningControl.");
System.err.println("There's no reason to go on for the purpose of this demo.");
return false;
}
Time duration = p.getDuration();
if (duration != Duration.DURATION_UNKNOWN) {
System.err.println("Movie duration: " + duration.getSeconds());
time=(int)duration.getSeconds();
totalFrames = fpc.mapTimeToFrame(duration);
if (totalFrames != FramePositioningControl.FRAME_UNKNOWN)
System.err.println("Total # of video frames in the movies: " + totalFrames);
else
System.err.println("The FramePositiongControl does not support mapTimeToFrame.");
} else {
System.err.println("Movie duration: unknown");
}
// Prefetch the player.播放器預(yù)取資源
p.prefetch();
if (!waitForState(p.Prefetched)) {
System.err.println("Failed to prefetch the player.");
return false;
}
// Display the visual & control component if there's one.
return true;
}
/**
* Block until the player has transitioned to the given state.
* Return false if the transition failed.
*/
boolean waitForState(int state) {
synchronized (waitSync) {
try {
while (p.getState() < state && stateTransitionOK)
waitSync.wait();
} catch (Exception e) {}
}
return stateTransitionOK;
}
public void controllerUpdate(ControllerEvent evt) {
if (evt instanceof ConfigureCompleteEvent ||
evt instanceof RealizeCompleteEvent ||
evt instanceof PrefetchCompleteEvent) {
synchronized (waitSync) {
stateTransitionOK = true;
waitSync.notifyAll();
}
} else if (evt instanceof ResourceUnavailableEvent) {
synchronized (waitSync) {
stateTransitionOK = false;
waitSync.notifyAll();
}
} else if (evt instanceof EndOfMediaEvent) {
p.setMediaTime(new Time(0));
//p.start();
//p.close();
//System.exit(0);
} else if (evt instanceof SizeChangeEvent) {
}
}
public int getTotal(){
return totalFrames;
}
public int getTime(){
return time;
}
public void initVF(){
if (totalFrames == FramePositioningControl.FRAME_UNKNOWN)
System.err.println("Cannot jump to a random frame.");
else {
int randomFrame = fpc.seek(1);
//System.err.println("Jump to a random frame: " + randomFrame);
}
int currentFrame = fpc.mapTimeToFrame(p.getMediaTime());
if (currentFrame != FramePositioningControl.FRAME_UNKNOWN ){
Buffer buf = fgc.grabFrame(); //截取媒體當(dāng)前播放的幀
vf = (VideoFormat) buf.getFormat();
}
}
public Image capture(int a) { //獲取當(dāng)前幀
Image im=null;
if (totalFrames == FramePositioningControl.FRAME_UNKNOWN)
System.err.println("Cannot jump to a random frame.");
else {
int randomFrame = fpc.seek(a);
//System.err.println("Jump to a random frame: " + randomFrame);
}
int currentFrame = fpc.mapTimeToFrame(p.getMediaTime());
if (currentFrame != FramePositioningControl.FRAME_UNKNOWN ){
//System.err.println("Current frame: " + currentFrame);
Buffer buf = fgc.grabFrame(); //截取媒體當(dāng)前播放的幀
//VideoFormat vf = (VideoFormat) buf.getFormat();
BufferToImage bti =new BufferToImage(vf);
//int wideth = vf.getSize().width;
//int height = vf.getSize().height;
im = bti.createImage(buf);
}
return im;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -