how to play video using java?


I want to play a video in media player using java i used JMF to display the video however it gives me an error in one of the data types i used from the JMF library and here is my code.

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;

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

        if (mediaURL != null) // only display if there is a valid URL
        {
            JFrame mediaTest = new JFrame("Media Tester");
            mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            MediaPlayer mediaPanel = new MediaPlayer(mediaURL);

            mediaTest.add(mediaPanel);

            mediaTest.setSize(300, 300);
            mediaTest.setVisible(true);
        } // end inner if
    } // end outer if
}

the problem is it gives me an error in this lines

MediaPlayer mediaPanel = new MediaPlayer(mediaURL);

The constructor MediaPlayer(URL) is undefined as the MediaPlayer constructor does not take anything but i need to add url to the mediaplayer to display it in the jframe but i cannot, any help ?


Answers:


You need to use setMediaLocation(location) to specify the location of the media to launch, so try this instead:

MediaPlayer mediaPanel = new MediaPlayer();
mediaPanel.setMediaLocation(mediaURL.toString());

Alternatively you can use setMediaLocator(locator) for the same purpose but a little bit more convenient in case you have an URL, the code would then be:

MediaPlayer mediaPanel = new MediaPlayer();
mediaPanel.setMediaLocator(new MediaLocator(mediaURL));