playing video using jmf

Ruby picture Ruby · Jun 19, 2012 · Viewed 12.9k times · Source

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);
        }
    }
}
}

The exception that I am getting is No media player found

Answer

Mikle Garin picture Mikle Garin · Jun 19, 2012

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

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. Use vlcj library to embed VLC video player into your Java-application

  2. USe JavaFX media player

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.

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

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.