播放音频& JFrame窗口中的视频

时间:2015-09-25 02:00:23

标签: java swing audio video jmf

我正在尝试将视频添加到JFrame窗口但遇到麻烦。我在网上发现了一些关于JMF的东西,但我觉得Javax对吗?我可以使用Javax并一起摆动吗?现在我在那里有一些代码没有用,一个是因为我需要一个Player类(或者是我缺少一个Player导入)而且我不确定需要去哪里,两个因为我认为JMF可能是Javax,我不知道这和摇摆是否可以一起使用?

我要做的是当按下按钮时,JFrame窗口被清除并弹出一个视频。我到了我试图添加视频的地方。难道这很难,很多代码或者像添加图片一样简单吗?我想在JFrame上添加一个音频片段,而不是我现在在那里使用的冗长的JLabel。

我根据相关代码设置了星标。

非常感谢正确方向的任何提示或指针!

public class MultiForm extends JFrame{

    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;
    private JLabel matrixQuote;
    private int matrixSelection;
    private JFrame newFrame;
    private JPanel panel;
    private JPanel panel2;
    private static String[] fileName = {"", "The Matrix", "Another Option"};


public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

    TheHandler handler = new TheHandler();
    menu.addItemListener(handler);  

}

public void matrixPanel() {

    TheHandler handler = new TheHandler();
    //Create a new window when "The Matrix" is clicked in the JCB
    newFrame = new JFrame();
    panel = new JPanel();
    panel2 = new JPanel();

    newFrame.setLayout(new FlowLayout());
    newFrame.setSize(500, 300);
    newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);      

    matrixQuote = new JLabel("<html>After this, there is no turning back. "
            + "<br>You take the blue pill—the story ends, you wake up "
            + "<br>in your bed and believe whatever you want to believe."
            + "<br>You take the red pill—you stay in Wonderland, and I show"
            + "<br>you how deep the rabbit hole goes. Remember: all I'm "
            + "<br>offering is the truth. Nothing more.</html>");

    panel.add(matrixQuote);
    newFrame.add(panel, BorderLayout.NORTH);

    //Blue pill button and picture. 
    Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
    bluePill = new JButton("Blue Pill", bp);
    panel2.add(bluePill);   
    bluePill.addActionListener(handler);

    //Red pill button and picture
    Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
    redPill = new JButton("Red Pill", rp);
    panel2.add(redPill);

    newFrame.add(panel2, BorderLayout.CENTER);      
    newFrame.setVisible(true);
}

private class TheHandler implements ItemListener, ActionListener{

    public void itemStateChanged(ItemEvent IE) {
        //listen for an item to be selected.
        if(IE.getStateChange() == ItemEvent.SELECTED) {
            Object selection = menu.getSelectedItem();

            if("The Matrix".equals(selection)) {
                matrixPanel();
            }
            else if("Another Option".equals(selection)) {   
            }
        }   
    }

    public void actionPerformed(ActionEvent AE) {
        if(AE.getSource() == bluePill) {
            newFrame.remove(panel);         
            newFrame.remove(panel2);
            newFrame.repaint();
*******************************************************************************         
            //TRYING TO ADD VIDEO TO THE NEWLY CLEARED JFRAME
            URL mediaURL = getClass().getResource("WAKE-UP.MP4");
            Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            //get component for video and the playback controls...I think
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlComponent();
            add(video, BorderLayout.CENTER);
            add(controls, BorderLayout.SOUTH);

        }
    }   
}

//MAIN
public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}

0 个答案:

没有答案