Delphine
Delphine

Reputation: 41

How can i play mp4 video file in a JFrame?

I want to play a video (mp4) inside a fixed size JFrame. How should I do this? Is there any tutorial or documentation for this?

Upvotes: 4

Views: 32109

Answers (5)

martin
martin

Reputation: 11

All you really need is a library called vlcj to play any kind of videos on JFrame, since JavaFX's MediaPlayer doesn't support .avi files.

Link to vlcj: https://github.com/caprica/vlcj

(There is also implementation for JavaFX: https://github.com/caprica/vlcj-javafx)

Upvotes: 1

Bill Nye
Bill Nye

Reputation: 871

I guess you could convert your video into a .gif file and then get the audio from that video and play it in the background, it may lower the quality but for simple things it should work.

`import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

    public class Main {

        public static void audio() {
            try {
                File file = new File("audio.wav");
                Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(file));
                clip.loop(Clip.LOOP_CONTINUOUSLY);
                clip.start();
            } catch (Exception e) {
                System.err.println("Put the music.wav file in the sound folder if you want to play background music, only optional!");
            }
        }


        private static String arg;

        public static void main(String[] args){


        arg = "background.gif";
        JFrame f = new JFrame();
        JPanel p = new JPanel();
        JLabel l = new JLabel();
        ImageIcon icon = new ImageIcon(arg);    
        f.setSize(480, 360);
        f.setVisible(true);
        l.setIcon(icon);
        p.add(l);
        f.getContentPane().add(p);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        audio();

            }
        }

https://www.mediafire.com/?18n4iluk9hwitlb Audio.wav

http://www.mediafire.com/view/jx7vogxzsbor0j4/background.gif Background.gif

Upvotes: 2

user2233433
user2233433

Reputation: 61

The JavaFX widget MediaView is able to render an MP4 file. You can embed JavaFX in Swing like this http://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm and then you can use the MediaView like this http://docs.oracle.com/javafx/2/media/simpleplayer.htm

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

The "fixed size(1) JFrame" is easy, or at least it should be. Are you having a specific problem with it? (I ask because it seems odd you should mention it.)

As to support for MP4, the JMF will not handle it. Your best bet is to see what Google throws up for java+mp4. After looking through a few of the top ranked hits, it seems the offerings are not great.

Is MP4 an unchangeable requirement? The JMF can handle many other simpler (older) formats well.

If opening the MP4 in the standard media player is not out of the question - that is as easy as:

Desktop.getDesktop().open(new File("the.mp4"));

(1) And as an end user, have to comment that I detest someone delivering video content to my desktop that cannot be resized. It's my (dang) desktop - I should be able to choose how much of it the video covers!

Upvotes: 1

const-ae
const-ae

Reputation: 2216

I think you have use a special framework to do that. You could for example use the Java Media Framework You can download it at the oracle homepage.

Upvotes: 1

Related Questions