Xuggler - 视频新的Runnable没有远见,也无法结束

时间:2013-09-05 16:58:01

标签: java swing video xuggler

我要做的是使用“播放”按钮制作JFrame。单击按钮时,应播放视频 视频结束后应关闭,应该可以再次单击该按钮。

我接近的问题是:
1.单击“播放”按钮后,会出现新的视频帧,但没有视图,只有黑色背景。音频播放正常 2.当视频结束时(通过声音判断),帧不会关闭,并且在“循环读取数据包”执行后没有任何内容。

我需要的是:
1.制作视频。
2.使视频结束,以便程序完成后可以进行其他工作。

对此事的任何帮助都将不胜感激。

以下是我使用的代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.IMediaViewer;
import com.xuggle.mediatool.ToolFactory;


public class FramePlayer extends JFrame {
    private String movieName = "movieName.avi";

    public FramePlayer() {
        //Create Panel
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);

        JButton startButton = new JButton("Play");
        startButton.setBounds(50, 60, 80, 30);

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {

            //Play movie on button click
            IMediaReader reader = ToolFactory.makeReader(movieName);
            IMediaViewer viewer = ToolFactory.makeViewer();
            reader.addListener(viewer);
            int i = 0;
            while(reader.readPacket() == null)
            {
                //Make sure video is playing
                i++;            
                System.out.println("Frame " + i);
            }
            //This should appear after video ends
            System.out.println("End of video");

           }
        });

        panel.add(startButton);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                FramePlayer fp = new FramePlayer();
                fp.setVisible(true);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

new Thread() {
    public void run() {
        //Play movie on button click
        IMediaReader reader = ToolFactory.makeReader(movieName);
        IMediaViewer viewer = ToolFactory.makeViewer();
        reader.addListener(viewer);
        int i = 0;
        while(reader.readPacket() == null)
        {
            //Make sure video is playing
            i++;            
            System.out.println("Frame " + i);
        }
        //This should appear after video ends
        System.out.println("End of video");
    }
}.start();