This discussion is archived
4 Replies Latest reply: Aug 24, 2012 7:05 AM by gimbal2 RSS

(Youtube-) Video in a Swing GUI

957810 Newbie
Currently Being Moderated
Hey everyone,

I'm currently trying to play a video in my Swing GUI with JMF but I really can't get it to work.
With the help of google I got this far:

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFrame;

public class MediaPanel extends JFrame {

    public MediaPanel() {
        setLayout(new BorderLayout()); // use a BorderLayout

        // Use lightweight components for Swing compatibility
        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
        
        URL mediaURL = null;
        
        try {
            mediaURL = new URL("http://www.youtube.com/watch?v=Q7_Z_mQUBa8");
        } catch (MalformedURLException ex) {
            System.err.println(ex);
        }
        try {
            // create a player to play the media specified in the URL
            Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);

            // get the components for the video and the playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();

            if (video != null) {
                add(video, BorderLayout.CENTER); // add video component
            }
            if (controls != null) {
                add(controls, BorderLayout.SOUTH); // add controls
            }
            mediaPlayer.start(); // start playing the media clip
        } // end try
        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");
        } // end catch
    } // end MediaPanel constructor
}
But all I get is errors:

Warning: The URL may not exist. Please check URL
No media player found


Can you please please help me get this working? I would really appreciate a little walkthrough
Best regards,
Patrick

Edited by: 954807 on Aug 24, 2012 6:52 AM
  • 1. Re: (Youtube-) Video in a Swing GUI
    957810 Newbie
    Currently Being Moderated
    Here's a little bit more readable version:

    Pastebin

    Edited by: 954807 on Aug 24, 2012 6:27 AM
  • 2. Re: (Youtube-) Video in a Swing GUI
    gimbal2 Guru
    Currently Being Moderated
    Just use \
     tags. People don't like to go to external sites.
    
    I really advise you to consider using JavaFX 2 here. Swing is old and not really supported anymore, JMF is also old and absolutely not supported anymore.                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 3. Re: (Youtube-) Video in a Swing GUI
    957810 Newbie
    Currently Being Moderated
    Great, thanks, didn't see those anywhere, changed it now.

    Can I put this in JavaFX2 and just call it with Swing? There's a lot of pre-existing stuff that I can absolutely not get rid of in the little time I have for this...
    It absolutely doesn't necessarily have to be JMF, you can just point me to the right direction where I can find a possibility to integrate the video, that would be really great! :)

    Thanks already very much

    Edited by: 954807 on Aug 24, 2012 7:02 AM
  • 4. Re: (Youtube-) Video in a Swing GUI
    gimbal2 Guru
    Currently Being Moderated
    There is in fact a way to integrate JavaFX 2 into a Swing application, yes.

    http://docs.oracle.com/javafx/2/swing/jfxpub-swing.htm

    (google: 'javafx 2 swing integration')

    Do note that JavaFX 2 is quite recent tech so your JVM should be up to date to be able to use it. JavaFX 2.2 now comes as part of Java 7 update 6, before that you'd need a separate installation of the JavaFX 2 runtime. I don't know who your target audience is but if its "just anyone out there" then the installation requirements may be a bottleneck for you.

    There is also FMJ, which is also a project with no more development behind it but at least its more recent than JMF (and API compatible).

    http://fmj-sf.net/

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points