0
我试图在使用JMF的Java应用程序中播放视频。视频播放良好,但我试图让视频更大。下面的代码被放置在另一个具有gridbag布局的jpanel中。使用JMF调整视频大小
我目前已经添加了没有FILL约束,所以它应该以正常大小显示。
当我添加一个填充约束它延长视频偏移纵横比。
我猜IM询问是否有人知道如何手动调整视频或如何锁定宽高比
public class VideoPanel extends JPanel{
private Player mediaPlayer;
private File file;
public VideoPanel(String videoFile, String path){
setOpaque(false);
file = new File(path+"/video/"+videoFile);
URL mediaURL = null;
try {
mediaURL = file.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
setLayout(new BorderLayout());
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
try{
mediaPlayer = Manager.createRealizedPlayer(mediaURL);
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
double scale = getScale(this.getWidth(),this.getHeight(),video.getWidth(),video.getHeight());
video.setPreferredSize(new Dimension((int)scale*video.getWidth(),(int)scale*video.getHeight()));
if(video != null)
add(video,BorderLayout.CENTER);
if(controls != null)
add(controls,BorderLayout.SOUTH);
}
catch (NoPlayerException noPlayerException){
System.err.println("No media player found");
}
catch (CannotRealizeException cannotRealizeException){
System.err.println("Could not realize media player");
}
catch (IOException iOException){
System.err.println("Error reading from the source");
}
}
private double getScale(int panelWidth, int panelHeight, int imageWidth, int imageHeight) {
double scale = 1;
double xScale;
double yScale;
xScale = (double) panelWidth/imageWidth;
yScale = (double) panelHeight/imageHeight;
scale = Math.min(xScale, yScale);
return scale;
}
public void stopPlay(){
mediaPlayer.stop();
}
}
有没有人解决了这个问题?我只是无法通过JMF播放视频(实际上我使用的是http://fmj-sf.net/,但API完全相同)。 – petersaints 2011-11-02 23:07:24