让Xuggler电影在JFrame中播放

时间:2014-09-25 22:07:39

标签: java swing xuggler

尝试让Xuggler影片在JFrame中播放,而不是在自己的窗口中播放。我按下提交按钮后使用CardLayout切换到Xuggler面板,但Xuggler影片在新窗口中打开,而不是在JFrame中打开。欢迎大家提出意见。谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaViewer;
import com.xuggle.mediatool.ToolFactory;

public class ViewerTest implements ActionListener {

    JFrame frame;
    JPanel panel_container;
    CardLayout cardLayout;
    MoviePanel panel_one;
    DecodeAndPlayVideo panel_two;

    ViewerTest() {
        panel_one = new MoviePanel();

        frame = new JFrame("Yosemite Movie Viewer");

        frame.setSize(700, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(new BorderLayout());

        JLabel label_heading = new JLabel("HalfDome Time-Lapse Viewer");
        label_heading.setForeground(Color.white);
        label_heading.setFont(new Font("Verdana", Font.BOLD, 16));

        // Set up the panel container and cardlayout
        cardLayout = new CardLayout();
        panel_container = new JPanel(cardLayout);
        panel_container.add(panel_one, "1");

        cardLayout.show(panel_container, "1");

        JPanel panel_bottom = new JPanel();
        JButton submitButton = new JButton("View Movie");
        submitButton.addActionListener(this);
        panel_bottom.add(submitButton);

        frame.add(panel_bottom, BorderLayout.SOUTH);
        frame.add(panel_container, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ViewerTest();
            }
        });
    }

    // Actions for when submit button is pressed
    public void actionPerformed(ActionEvent ae) {

        if (ae.getActionCommand().equals("View Movie")) {

            panel_two = new DecodeAndPlayVideo();
            panel_container.add(panel_two, "2");
            cardLayout.show(panel_container, "2");

        }
    }
}

class DecodeAndPlayVideo extends JPanel {

    IMediaReader reader;

    // Constructor that plays a video in a JPanel
    DecodeAndPlayVideo() {

        this.setBackground(Color.RED);
        String fileName = "/Users/jorma/Code/EclipseWS/YosemiteMovieViewer/res/gp_2012_04_24.mov";

        if (fileName.length() <= 0)
            throw new IllegalArgumentException(
                    "must pass in a filename as the first argument");

        // create a new reader
        reader = ToolFactory.makeReader(fileName);
        //
        // Create a MediaViewer object and tell it to play video only
        //
        reader.addListener(ToolFactory.makeViewer(IMediaViewer.Mode.VIDEO_ONLY));

        // read out the contents of the media file, and sit back and watch
        while (reader.readPacket() == null)
            do {
                System.out.println("Playing movie");
            } while (false);
    }
}

0 个答案:

没有答案