سؤال

What i'm trying to do is to make a JFrame with button "Play". When button is clicked there should be played video.
After video ends it should close and it should be possible to click the button again.

The problems i approach are:
1. After clicking "Play" button there appears new frame for video but there is no view, only black background. Audio plays normal.
2. When video comes to an end (judging by sound), frame doesn't close and nothing after "reading packets while loop" executes.

What i need is to:
1. Make video appear.
2. Make video end so program can make other work after it finishes.

Any help in this matter would be appreciated.

Below is the code i use.

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);
            }
        });
    }
}
هل كانت مفيدة؟

المحلول

Try This:

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top