A simple way of embedding a video in my Swing GUI

2019-03-11 15:10发布

问题:

I've been looking for a while now for a dead simple way of embedding a video into my Java Swing GUI. Am I chasing after the wind? Ideally, I would love to have something like:

VideoPlayer video = new VideoPlayer("filename");
frame.getContentPane().add(video);
video.play();

Am I searching for something that doesn't exist? I've developing mainly for linux but with windows in mind as I might try and make my application cross platform in the future.

Addtional information:

  • I've looked at JMF before and was unpleased at the amount of code needed before a video could actually be displayed and played. I might visit it again.
  • I thought of an embedded browser that would play a video using VLC, but again not the easiest thing ever.
  • I have complete control on the format of the videos to be played. They are fixed in number and can be recoded if needed.

回答1:

I dont know why you think you need a lot of code to use JMF.


    public class mediaPlayer extends JFrame
    {
        public mediaPlayer()
        {
            setLayout(new BorderLayout());

            //file you want to play
            URL mediaURL = //Whatever
            //create the media player with the media url
            Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
            //get components for video and playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();
            add(video,BorderLayout.CENTER);
            add(controls,BorderLayout.SOUTH);
        }
    }

A complete media player in like 6 lines, prob could have done it in less. If all you need is something basic, then id go with JMF.

As Zemzela mentioned, Xuggle is also a good solution but will require more work.

There are also Java bindings VLC. Click Here



回答2:

you can use xuggle. This is their site http://www.xuggle.com/. I have use it to display avi(divx) and works "fine". JMF i little bit slow in comparation to xuggle. Problem in java is that you can't accurate sync every frame on windows because Thread.sleep(30) doesn't sleep precisely 30 msec, and minimum how much can sleep in windows OS is 16 msec so you can't tune it to be approximately 30 msec. On linux should work more accurately, I think minimum sleeping time is 1 msec.