简体   繁体   English

使用jmf播放视频

[英]playing video using jmf

I am trying to play a video file using JMF but 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

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 vlcj library to embed VLC video player into your Java-application 使用vlcj库将VLC视频播放器嵌入到Java应用程序中

  2. USe JavaFX media player 使用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 Player嵌入到您的应用程序中-这可能是您的选择。

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. 无论如何,这是您的选择电话。

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

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