Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: How to play a video into a java application

  1. #1
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default How to play a video into a java application

    Hi guys,
    I'm new in this forum, so I would ask you how to insert and play a video or a music background in a java application. Let me explain better... I need to develop a game where sometime I need to show picture, or play videos and also, if is possible, add a music in background... I work with eclipse and I know quite good the main concept of java's language...
    I tried to search on google a way to solve my problem, using jmf or vlcj, but my problem is when I had copied the code that I found on internet I got some error...
    Could you help me?=)

    Sorry for my bad english =(

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to play a video into a java application

    Sounds like you want to be looking into using JavaFX 2 then.

    For the future: "I got some error" explains nothing so if you only provide that you cannot be helped.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: How to play a video into a java application

    Quote Originally Posted by gimbal2 View Post
    Sounds like you want to be looking into using JavaFX 2 then.
    Why not Java FX 8?

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to play a video into a java application

    I'm stuck in the past when it comes to naming. I should just say "the latest" ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Quote Originally Posted by gimbal2 View Post

    For the future: "I got some error" explains nothing so if you only provide that you cannot be helped.
    Sorry, you are right So, I copied this code that I found out on Internet but I get always the same errors...
    This is the code:

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    import javax.media.*;

    public class MediaPlayerDemo extends JFrame {
    private Player player;
    private File file;

    public MediaPlayerDemo()
    {
    super( "Demonstrating the Java Media Player" );

    JButton openFile = new JButton( "Open file to play" );
    openFile.addActionListener(
    new ActionListener() {
    public void actionPerformed( ActionEvent e )
    {
    openFile();
    createPlayer();
    }
    }
    );
    getContentPane().add( openFile, BorderLayout.NORTH );

    setSize( 300, 300 );
    show();
    }

    private void openFile()
    {
    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setFileSelectionMode(
    JFileChooser.FILES_ONLY );
    int result = fileChooser.showOpenDialog( this );

    // user clicked Cancel button on dialog
    if ( result == JFileChooser.CANCEL_OPTION )
    file = null;
    else
    file = fileChooser.getSelectedFile();
    }

    private void createPlayer()
    {
    if ( file == null )
    return;

    removePreviousPlayer();

    try {
    // create a new player and add listener
    player = Manager.createPlayer( file.toURL() );
    player.addControllerListener( new EventHandler() );
    player.start(); // start player
    }
    catch ( Exception e ){
    JOptionPane.showMessageDialog( this,
    "Invalid file or location", "Error loading file",
    JOptionPane.ERROR_MESSAGE );
    }
    }

    private void removePreviousPlayer()
    {
    if ( player == null )
    return;

    player.close();

    Component visual = player.getVisualComponent();
    Component control = player.getControlPanelComponent();

    Container c = getContentPane();

    if ( visual != null )
    c.remove( visual );

    if ( control != null )
    c.remove( control );
    }

    public static void main(String args[])
    {
    MediaPlayerDemo app = new MediaPlayerDemo();

    app.addWindowListener(
    new WindowAdapter() {
    public void windowClosing( WindowEvent e )
    {
    System.exit(0);
    }
    }
    );
    }

    // inner class to handler events from media player
    private class EventHandler implements ControllerListener {
    public void controllerUpdate( ControllerEvent e ) {
    if ( e instanceof RealizeCompleteEvent ) {
    Container c = getContentPane();

    // load Visual and Control components if they exist
    Component visualComponent =
    player.getVisualComponent();

    if ( visualComponent != null )
    c.add( visualComponent, BorderLayout.CENTER );

    Component controlsComponent =
    player.getControlPanelComponent();

    if ( controlsComponent != null )
    c.add( controlsComponent, BorderLayout.SOUTH );

    c.doLayout();
    }
    }
    }
    }

    After I choose my media file in the console appears this: http://puu.sh/fJWbb/4ccfdbba67.png

    I read something about JavaFX but I have no time to learn how to use it... There is another way to play video without JavaFX?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: How to play a video into a java application

    another way to play video without JavaFX?
    The much older package: JMF has some video playing capability. I think the package: javax.media is part of JMF
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to play a video into a java application

    So, I copied this code that I found out on Internet but I get always the same errors...
    That's normal behavior with copy/paste programming. Don't do that. Learn, understand and write your own code.
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Quote Originally Posted by gimbal2 View Post
    That's normal behavior with copy/paste programming. Don't do that. Learn, understand and write your own code.
    Yes, usually I do in this way, but I don't know anything about JMF , so I would like to understand why a got that errors... Could be the kind of file that I tried to open? I tried with .avi and .mp4, but same time the same errors...

  9. #9
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Hi guys, sorry if it was lasted long time from when I post this, but now the problem of the videos is very important for me... Let me explain better... I'm developing a simple game for a school project... The code is almost ready but I need to play some videos into the application... In particular the game starts in full screen mode, so I have to put the videos into a JPanel or similar, my problem is that I know only basic java and something about Swing, instead, for the rest usually I try to found out on Google... About this I didn't find anything that works... So can you help me? Thank you very much for your patience

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to play a video into a java application

    Looking through your stack trace (it would have been easier for us if you had copy/pasted it here) you are getting an error with FMJ, which is the open source bit of JMF.
    The error is the second one on this page, which explains what you need to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Quote Originally Posted by Tolls View Post
    Looking through your stack trace (it would have been easier for us if you had copy/pasted it here) you are getting an error with FMJ, which is the open source bit of JMF.
    The error is the second one on this page, which explains what you need to do.
    The errors that I got wasn't written in that page, so probably the problem might be the way of installation or the format of the video( I used an .mpg that I found with the code that I copied)... I downloaded the fmj files from: Confidentialité- France... Then I put the 3 jar file ( fmj.jar, fmj-nojmf, fmj-applet) into eclipse's project and I add them to build path... then I found out this code ... and I use it without modifies: the code is:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.io.IOException;
    import java.net.URL;
    import javax.media.CannotRealizeException;
    import javax.media.Manager;
    import javax.media.NoPlayerException;
    import javax.media.Player;
    import javax.swing.JPanel;
       
    public class MediaPanel extends JPanel
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	public MediaPanel( URL mediaURL )
        {
    		setLayout( new BorderLayout() ); // use a BorderLayout
       
            // Use lightweight components for Swing compatibility
    		Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
    		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
    } // end class MediaPanel
    and this is the main:
    Java Code:
    public class MediaTest
    {
    	// launch the application
        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 );
       
                	MediaPanel mediaPanel = new MediaPanel( mediaURL );
                	mediaTest.add( mediaPanel );
      
                	mediaTest.setSize( 300, 300 );
                	mediaTest.setVisible( true );
    			} // end inner if
    		} // end outer if
    	} // end main
    } // end class MediaTest
    Then I tried to run the program,but whan I selected my video there were errors like this:
    Java Code:
    feb 25, 2015 5:36:24 PM net.sf.fmj.ds.media.content.unknown.Handler setSource
    INFORMAZIONI: Path: D:\Utenti\XxMK98xX\Download\bailey.mpg
    feb 25, 2015 5:36:24 PM net.sf.fmj.ds.media.content.unknown.Handler setSource
    AVVERTENZA: java.lang.UnsatisfiedLinkError: no jdshow in java.library.path
    java.lang.UnsatisfiedLinkError: no jdshow in java.library.path
    	at java.lang.ClassLoader.loadLibrary(Unknown Source)
    	at java.lang.Runtime.loadLibrary0(Unknown Source)
    	at java.lang.System.loadLibrary(Unknown Source)
    	at net.sf.fmj.ds.media.content.unknown.Handler.setSource(Handler.java:82)
    	at javax.media.Manager.createPlayer(Manager.java:178)
    	at javax.media.Manager.createPlayer(Manager.java:247)
    	at javax.media.Manager.createPlayer(Manager.java:103)
    	at javax.media.Manager.createPlayer(Manager.java:83)
    	at javax.media.Manager.createRealizedPlayer(Manager.java:255)
    	at prova.MediaPanel.<init>(MediaPanel.java:29)
    	at prova.MediaTest.main(MediaTest.java:37)
    
    feb 25, 2015 5:36:24 PM net.sf.fmj.ds.media.content.unknown.Handler setSource
    INFORMAZIONI: Path: D:\Utenti\XxMK98xX\Download\bailey.mpg
    feb 25, 2015 5:36:24 PM net.sf.fmj.ds.media.content.unknown.Handler setSource
    AVVERTENZA: java.lang.UnsatisfiedLinkError: no jdshow in java.library.path
    java.lang.UnsatisfiedLinkError: no jdshow in java.library.path
    	at java.lang.ClassLoader.loadLibrary(Unknown Source)
    	at java.lang.Runtime.loadLibrary0(Unknown Source)
    	at java.lang.System.loadLibrary(Unknown Source)
    	at net.sf.fmj.ds.media.content.unknown.Handler.setSource(Handler.java:82)
    	at javax.media.Manager.createPlayer(Manager.java:178)
    	at javax.media.Manager.createPlayer(Manager.java:247)
    	at javax.media.Manager.createPlayer(Manager.java:159)
    	at javax.media.Manager.createPlayer(Manager.java:83)
    	at javax.media.Manager.createRealizedPlayer(Manager.java:255)
    	at prova.MediaPanel.<init>(MediaPanel.java:29)
    	at prova.MediaTest.main(MediaTest.java:37)
    
    No media player found

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to play a video into a java application

    Yes it is on that page.
    This is in your stack trace:
    java.lang.UnsatisfiedLinkError: no jdshow in java.library.path

    The title of the second Troubleshooting Tip is:
    java.lang.UnsatisfiedLinkError (jdshow)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: How to play a video into a java application

    Quote Originally Posted by Tolls View Post
    Looking through your stack trace (it would have been easier for us if you had copy/pasted it here) you are getting an error with FMJ, which is the open source bit of JMF.
    Actually it has nothing to do with JMF; its simply an alternative to it as the main page states in bold:

    Home - FMJ

    JMF went to sleep a long time ago and that is when FMJ was born. And now FMJ has gone to sleep a long time ago too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to play a video into a java application

    Oh right.
    I think I might have known that...or maybe not.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Hi guys, sorry if it was lasted long time from when I replied last time... I used VLCJ for playing video, but I have many problem, first of all, VLCJ needs vlc plugin, and I don't know how can I put it into deploy machine... So, I took a look to javafx media player... It is really good... but I don't understand how can I use it into my application... now I'm using swing components... Can I play videos into, for example, a JPanel? I prefer if the videos are inside my project instead of use a relative path.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: How to play a video into a java application

    I prefer if the videos are inside my project instead of use a relative path.
    It depends on the requirements of the class and method that reads the input. If it can read from a Stream or URL then the file could be in the jar. If it requires a File object, it can't be read from inside a jar file.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Quote Originally Posted by Norm View Post
    It depends on the requirements of the class and method that reads the input. If it can read from a Stream or URL then the file could be in the jar. If it requires a File object, it can't be read from inside a jar file.
    OK, I understand this... My java application runs in full screen exclusive mode, and when I need to play videos I nave to exit from full screen and after the video's end I get back in full screen ... I have to do this because with VLCJ i can't play videos in full screen exclusive mode... ( my application runs in full screen exclusive mod in 1024x768 to fit every deploy machines)... Now, with javafx I can do something similar? But I don't want to exit from full screen I prefer use a jlayerpane and pur the video up everything

  18. #18
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    up
    Can you do for me a demo code using javafx for play media in a swing application?

  19. #19
    XxMK98xX is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to play a video into a java application

    Quote Originally Posted by XxMK98xX View Post
    up
    Can you do for me a demo code using javafx for play media in a swing application?
    uppp

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

Page 1 of 2 12 LastLast

Similar Threads

  1. how to play youtube video in java application
    By usuikazkou in forum New To Java
    Replies: 1
    Last Post: 05-29-2014, 08:48 PM
  2. Using JMF to play a video inside java code
    By esl21 in forum Java Applets
    Replies: 1
    Last Post: 12-08-2011, 01:54 AM
  3. how to play a video from a file using jmf in a java
    By 200808147 in forum New To Java
    Replies: 0
    Last Post: 07-10-2011, 01:20 PM
  4. How to play video files in Java
    By Hasan in forum Advanced Java
    Replies: 11
    Last Post: 04-27-2009, 12:57 AM
  5. how to play MIDI in java application?
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •