我的视频播放器周围有黑条,如下图所示。我如何摆脱它们?我尝试在AVPlayer类中固定大小,但是尽管这停止了视频播放器的随机调整大小,但它并没有消除黑条。
我的第二个问题:如何制作,使视频播放器坐在现在的位置,但向上移动一点,以便播放器的顶部与组合框位于同一行?现在,它与显示“英语”的JLabel位于同一行,但我希望它在两行中都扩展。
在下面,您将找到我的SSCCE。它仍然有点长,但我摆脱了所有依赖关系。如果编译它,您应该得到上面的图像。我将AVPlayer类作为内部类来简化代码,它实际上具有自己的类。您需要为视频播放器添加视频源才能显示。
public class SSCCE extends JPanel {
private final JLabel kanjiLabel;
private final JLabel englishLabel;
private final JPanel wordPanel, cbPanel;
private final JComboBox chapters;
private final DefaultComboBoxModel model;
private final JLabel chapLabel = new JLabel("Chapter");
private AVPlayer player;
private final GridBagConstraints constraints = new GridBagConstraints();
public SSCCE() {
setLayout(new GridBagLayout());
setConstraints();
//wordPanel
wordPanel = new JPanel();
wordPanel.setLayout(new BoxLayout(wordPanel, BoxLayout.Y_AXIS));
wordPanel.setPreferredSize(new Dimension(500, 150));
kanjiLabel = new JLabel();
englishLabel = new JLabel();
kanjiLabel.setFont(new java.awt.Font("Dialog", 0, 50));
englishLabel.setFont(new java.awt.Font("Dialog", 0, 40));
wordPanel.add(englishLabel);
wordPanel.add(Box.createRigidArea(new Dimension(10, 10)));
wordPanel.add(kanjiLabel);
//chapter combox
chapters = new JComboBox();
model = new DefaultComboBoxModel();
chapters.setModel(model);
cbPanel = new JPanel();
addCB();
constraints.gridy += 1;
add(wordPanel, constraints);
//add the audio video panel
player = new AVPlayer();
constraints.gridx += 1;
add(player, constraints);
}
private void setConstraints() {
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
}
private void addCB() {
setConstraints();
cbPanel.removeAll();
cbPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
cbPanel.add(chapLabel);
cbPanel.add(chapters);
add(cbPanel, constraints);
}
public void displayNewWord() {
kanjiLabel.setText("Kanji");
englishLabel.setText("English");
player.playVideo("video.mp4"); //replace this with a video on your computer
revalidate();
repaint();
}
public class AVPlayer extends JPanel {
private static final String VLCPATH = "C:\\VideoLAN\\VLC";
private final MediaPlayerFactory mpf;
private final EmbeddedMediaPlayer emp;
public AVPlayer() {
//this panels sizes
setPreferredSize(new Dimension(150, 150));
setMinimumSize(new Dimension(150, 150));
setMaximumSize(new Dimension(150, 150));
//canvas
setLayout(new BorderLayout());
Canvas canvas = new Canvas();
canvas.setPreferredSize(new Dimension(150, 150));
add(canvas, BorderLayout.CENTER);
//load the native vlc library
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), VLCPATH);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
mpf = new MediaPlayerFactory();
emp = mpf.newEmbeddedMediaPlayer();
emp.setVideoSurface(mpf.newVideoSurface(canvas));
}
public void playVideo(String path) {
emp.prepareMedia(path);
emp.play();
emp.setRepeat(true);
}
}
}
和主要方法
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SSCCE sscce = new SSCCE();
frame.add(sscce, BorderLayout.NORTH);
frame.setVisible(true);
sscce.displayNewWord();
frame.revalidate();
最佳答案
黑条是嵌入式VLC媒体播放器输出的视频的一部分。
您需要设置适当的宽高比和/或裁剪几何形状以摆脱它们。
例如:
mediaPlayer.setCropGeometry("4:3"); // W:H
mediaPlayer.setCropGeometry("719x575+0+0"); // WxH+L+T
mediaPlayer.setCropGeometry("6+10+6+10"); // L+T+R+B
mediaPlayer.setAspectRatio("185:100");
只有您知道正确的值应该是什么。
或者,如果本机视频输出中不存在黑条,则这是由于您的
Canvas
大小与视频的宽高比不同。您可以通过将
Canvas
背景设置为其他颜色来解决这两个问题中的哪一个。