java 使用 jmf 播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11097174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:46:22  来源:igfitidea点击:

playing video using jmf

javaswingvideojmf

提问by Ruby

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

我正在尝试使用播放视频文件,JMF但它给了我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);
        }
    }
}
}

The exception that I am getting is No media player found

我得到的例外是 No media player found

回答by Mikle Garin

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.

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

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. Use vlcjlibrary to embed VLCvideo player into your Java-application

  2. USe JavaFX media player

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

  2. 使用 JavaFX媒体播放器

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.

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

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

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

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).

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

Anyway, it is your call what to choose.

不管怎样,选择什么是你的事。