JMC / JavaFX - 播放视频

发布于 2024-10-27 11:50:20 字数 2315 浏览 1 评论 0原文

我尝试创建一个小应用程序,只播放硬盘上的视频。我已经尝试了三天了,但我不知道该怎么做。网上没有好的教程或示例来使用当前的 javafx (jmc) 版本来执行此操作。我想创建一个 swing 应用程序,它使用 javafx 中的 jmc 类。我尝试了这个:

 ...
 MediaProvider mp;
 String mediaURI = "G:\\teste2.avi";
 JFrame jf = new JFrame();

JPanel j = new JPanel();
j.setLayout(new BorderLayout());

    mp = new MediaProvider();
    try {
        mp.setSource(new URL("file://" + mediaURI).toURI());
    } catch (MalformedURLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }catch (URISyntaxException ex2) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex2);
    }

 mp.setRepeating(false);
 j.setSize(800, 600);
 j.setVisible(true);
 jf.add(j);
 jf.setSize(800, 600);
 j.setBackground(Color.red);
 jf.setVisible(true);
 mp.play();
 ...

但是现在:如何将“mp”添加到我的 jpanel 中? “j.add(mp);”不起作用(jpanel 仅是红色,因为 color.red,但没有显示视频)。有简单的方法吗?谢谢。

我还尝试过第二个类:

public class Player implements VideoRendererListener{

private MediaProvider prov; //This is the most important class!

private VideoRenderControl renderer; //It's a interface to control the rendering

private Graphics2D ig;

private JPanel panel;


public void Player(File path, JPanel panel) {
    ig = (Graphics2D) panel.getGraphics();
    this.panel = panel;
    prov = new MediaProvider(path.toURI());
    renderer = prov.getControl(VideoRenderControl.class);
    renderer.addVideoRendererListener(this);
    prov.play();
    System.out.println(prov.getDuration());

}

@Override
public void videoFrameUpdated(VideoRendererEvent arg0) {
        float ratio = renderer.getFrameHeight() / (float)renderer.getFrameWidth();

        int diff = ( panel.getHeight() - Math.round(ratio * panel.getHeight())) / 2;

        System.out.println(renderer.getFrameHeight());


      /*  renderer.paintVideo(ig,
                new Rectangle(0, 0, renderer.getFrameWidth(), renderer.getFrameHeight())
                ,
                new Rectangle(0, diff, panel.getWidth(), Math.round(ratio * panel.getHeight())));
*/
}

}

并将其添加到主类中的 jpanel 中:

Player p = new Player();
p.Player(f,j);

但是“renderer.paintVideo()”方法不可用:(所以这也不起作用(可能使用旧版本的 jmc ,因为我在互联网上找到了这个例子)。

有谁知道如何使用当前版本的 javafx、jmc 将本地视频添加到 swing 应用程序中?

I've tried to create a small application, to just play a video from my hdd. I've tried for 3 days now but I don't know, how to do it. There are no good tutorials or examples on the net, to do this with the current javafx (jmc) release. I wanna create a swing application, that is using the jmc classes from javafx. I tried this:

 ...
 MediaProvider mp;
 String mediaURI = "G:\\teste2.avi";
 JFrame jf = new JFrame();

JPanel j = new JPanel();
j.setLayout(new BorderLayout());

    mp = new MediaProvider();
    try {
        mp.setSource(new URL("file://" + mediaURI).toURI());
    } catch (MalformedURLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }catch (URISyntaxException ex2) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex2);
    }

 mp.setRepeating(false);
 j.setSize(800, 600);
 j.setVisible(true);
 jf.add(j);
 jf.setSize(800, 600);
 j.setBackground(Color.red);
 jf.setVisible(true);
 mp.play();
 ...

But now: How to add the "mp" to my jpanel? "j.add(mp);" doesn't work (jpanel is red only, because of the color.red, but no video is shown). Is there an easy way to do it? Thank you.

I've also tried with a second class:

public class Player implements VideoRendererListener{

private MediaProvider prov; //This is the most important class!

private VideoRenderControl renderer; //It's a interface to control the rendering

private Graphics2D ig;

private JPanel panel;


public void Player(File path, JPanel panel) {
    ig = (Graphics2D) panel.getGraphics();
    this.panel = panel;
    prov = new MediaProvider(path.toURI());
    renderer = prov.getControl(VideoRenderControl.class);
    renderer.addVideoRendererListener(this);
    prov.play();
    System.out.println(prov.getDuration());

}

@Override
public void videoFrameUpdated(VideoRendererEvent arg0) {
        float ratio = renderer.getFrameHeight() / (float)renderer.getFrameWidth();

        int diff = ( panel.getHeight() - Math.round(ratio * panel.getHeight())) / 2;

        System.out.println(renderer.getFrameHeight());


      /*  renderer.paintVideo(ig,
                new Rectangle(0, 0, renderer.getFrameWidth(), renderer.getFrameHeight())
                ,
                new Rectangle(0, diff, panel.getWidth(), Math.round(ratio * panel.getHeight())));
*/
}

}

and adding this to my jpanel in main class:

Player p = new Player();
p.Player(f,j);

but the "renderer.paintVideo()" method isn't available :( so also this is not working (maybe with an old version of jmc, because i found this on the internet as example).

Does anyone know, how to add a local video to a swing application with the current release of javafx, jmc ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2024-11-03 11:50:20

我还在搜索如何播放视频,发现了这个:

http: //www.informit.com/articles/article.aspx?p=1326515&seqNum=4

检查对我有用的“清单 3 XMP2.java”。

问候。

I also searching about how to play video and I found this:

http://www.informit.com/articles/article.aspx?p=1326515&seqNum=4

Check the "Listing 3 XMP2.java" that work for me.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文