Search code examples
javavideographics2d

Play video with java


Here's the problem: I need to play a video (any kind: avi, wav, etc) with Java. I tried the JMF API some days ago but it won't play any video. I've since heard it's buggy and out of support, so I've given up on it. I've also tried jffmpeg but it's too heavy, because I only want to play a little presentation before a program runs. Is there some other way to do this, another library I could try?


Solution

  • JMF is super easy if all you want to do is play simple videos.

    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);
        }
    }
    

    Boom! You're whole video player in like 15 lines. Just make sure you install JMF. The harsh truth is video is not a simple matter and has poor support in Java.