简体   繁体   English

如何在 JFrame 中播放 mp4 视频文件?

[英]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?我想播放固定大小 JFrame 内的视频 (mp4)。我应该怎么做? Is there any tutorial or documentation for this?有没有这方面的教程或文档?

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.我猜你可以将你的视频转换成一个.gif 文件,然后从该视频中获取音频并在后台播放,这可能会降低质量,但对于简单的事情来说它应该可以工作。

`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 https://www.mediafire.com/?18n4iluk9hwitlb Audio.wav

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

The JavaFX widget MediaView is able to render an MP4 file. JavaFX 小部件 MediaView 能够呈现 MP4 文件。 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 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/媒体/simpleplayer.htm

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.例如,您可以使用Java 媒体框架。您可以在 oracle 主页下载它。

The "fixed size(1) JFrame" is easy, or at least it should be. “fixed size(1) JFrame”很简单,或者至少应该是这样。 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.至于对MP4的支持,JMF不会处理。 Your best bet is to see what Google throws up for java+mp4 .您最好的选择是查看 Google 为java+mp4抛出的内容。 After looking through a few of the top ranked hits, it seems the offerings are not great.在浏览了一些排名靠前的热门歌曲之后,这些产品似乎并不是很好。

Is MP4 an unchangeable requirement? MP4 是不变的要求吗? The JMF can handle many other simpler (older) formats well. JMF 可以很好地处理许多其他更简单(较旧)的格式。

If opening the MP4 in the standard media player is not out of the question - that is as easy as:如果在标准媒体播放器中打开 MP4 不是不可能的——那就很简单:

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. (1) 作为最终用户,我不得不说我讨厌有人向我的桌面发送无法调整大小的视频内容。 It's my (dang) desktop - I should be able to choose how much of it the video covers!这是我的(该死的)桌面——我应该可以选择视频涵盖的内容!

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.您真正需要的只是一个名为 vlcj 的库,可以在 JFrame 上播放任何类型的视频,因为 JavaFX 的 MediaPlayer 不支持 .avi 文件。

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

(There is also implementation for JavaFX: https://github.com/caprica/vlcj-javafx ) (还有 JavaFX 的实现: https://github.com/caprica/vlcj-javafx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM