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