使用 jmf 播放视频 [英] playing video using jmf

查看:28
本文介绍了使用 jmf 播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JMF 播放视频文件,但它给了我 找不到媒体播放器异常.

I am trying to play a video file using JMF but it gives me No Media Player found exception.

这是我的代码,谁能告诉我我在这里做错了什么?

Here is my code, can anyone tell me what I am doing wrong here?

public class MediaPanel extends JPanel {
public MediaPanel(URL mediaURL) {
    setLayout(new BorderLayout());

    try {
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
        Component video = mediaPlayer.getVisualComponent();
        Component controls = mediaPlayer.getControlPanelComponent();

        if (video != null)

            add(video, BorderLayout.CENTER);

        if (controls != null)
            add(controls, BorderLayout.SOUTH);

        mediaPlayer.start();
    } catch (NoPlayerException noPlayerException) {
        System.err.println("No media player found");
    } // end catch
    catch (CannotRealizeException cannotRealizeException) {
        System.err.println("Could not realize media player");
    } // end catch
    catch (IOException iOException) {
        System.err.println("Error reading from the source");
    }
}
}



public class MediaTest {

public static void main(String args[]) {
    // create a file chooser
    JFileChooser fileChooser = new JFileChooser();

    // show open file dialog
    int result = fileChooser.showOpenDialog(null);

    if (result == JFileChooser.APPROVE_OPTION) // user chose a file
    {
        URL mediaURL = null;
        Player mediaPlayer = null;

        try {
            // get the file as URL 
            mediaURL = fileChooser.getSelectedFile().toURL();
        } catch (MalformedURLException malformedURLException) {
            System.err.println("Could not create URL for the file");
        }

        if (mediaURL != null) {
            JFrame mediaTest = new JFrame("Media Tester");
            mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            MediaPanel mediaPanel = new MediaPanel(mediaURL);
            mediaTest.add(mediaPanel);

            mediaTest.setSize(300, 300);
            mediaTest.setVisible(true);
        }
    }
}
}

我得到的例外是找不到媒体播放器

推荐答案

您想播放什么类型的视频?JMF 是一个非常古老的库,无法播放大多数现代视频格式,只能播放一些旧格式(我什至不确定是哪些).

What kind of video are you trying to play? JMF is a pretty old library and won't be able to play most of modern video formats, only a few old ones (i am not even sure which ones).

实际上,如果我是对的,要播放特定的内容,您必须将自己的视频编码器编写/添加到 JMF 中,或者至少下载并使用通常已过时的现有视频编码器.

Actually, if I am right, to play something specific you will have to write/add your own video-encoders into JMF or at least download and use existing ones, which are usually outdated.

如果您真的想拥有可以播放任何现代视频的可调视频播放器之类的东西,有两种选择(在我看来):

If you really want to have something like tunable video player that could play any modern video there are two options (in my opinion):

  1. 使用 vlcj 库嵌入 VLC 视频播放器到您的 Java 应用程序

  1. Use vlcj library to embed VLC video player into your Java-application

使用 JavaFX 媒体播放器

我只提供这两个,因为前段时间我已经挖掘了大量的图书馆,没有其他任何东西甚至接近这两个.此外,大多数其他库以及 JMF 本身已经过时,这两个库正在频繁更新并得到大量用户的支持,因此这两个库是最佳选择.

I am offering only those two because I have dig through tons of libraries some time ago and there were nothing else even close to these two. Plus most of other libraries are outdated as well as JMF itself and these two are getting frequent updates and are supported with lots of users so those two are the best choice.

如果您不介意将 Java FX 播放器嵌入您的应用程序 - 这可能是您的选择.

In case you don't mind embedding Java FX player into your application - that might be your choice.

另一方面 - vlcj 稳定且易于集成到 Swing 应用程序中(它不像 Java FX 那样难,但 vlcj 在某些情况下可能更好).

On the other hand - vlcj is stable and easily integrated into Swing applications (its not like its hard with Java FX, but vlcj might be better for some cases).

不管怎样,选择什么由你决定.

Anyway, it is your call what to choose.

这篇关于使用 jmf 播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆