This discussion is archived
10 Replies Latest reply: Feb 5, 2013 1:43 PM by jsmith RSS

Loading mp4 files

colin-java Newbie
Currently Being Moderated
Hi, I have a javafx program.

It has an mp4 file located at Slider/src/slider/universe.mp4

where slider is the main package of the project, in which all the classes are.

When running file from within netbeans 7.2.1,
I am able to load the mp4 file using:

media = new Media(new File("src/slider/universe.mp4").toURI().toURL().toExternalForm().toString());

This works well, and the movie repeats over and over due to:

MediaPlayer player = new MediaPlayer(media);
player.setCycleCount(MediaPlayer.INDEFINITE);

So my problem is: When building the project, I get a jar file, but when running the jar file, it has a problem finding the file.
Basically with the code above, with the jar on my desktop, it gives the exception:

Caused by: MediaException: MEDIA_UNAVAILABLE : C:\Users\colin\Desktop\src\slider\universe.mp4 (The system cannot find the path specified)

And that obviously isn't right.

Can anyone help me get it to work, also I'd like it so the movie repeats, I managed to get it to play through jar file using
media = new Media(this.getClass().getResource("src\slider\universe.mp4").toExternalForm()); But it doesn't repeat using this.

Thanks for any help.
  • 1. Re: Loading mp4 files
    805536 Journeyer
    Currently Being Moderated
    For future reference, put code between \
    \
    tags.

    new File("src/slider/universe.mp4").toURI().toURL() will return a path to somewhere on the file system, not something from the JAR. Also, "src/slider/universe.mp4" is a relative path, meaning that the program will choose a url path based on the location of its own base URL.

    To access a file inside of a JAR, you should do this:
    //Get access to the universe.mp4 file inside of the slider package.
    Media media = new Media(Main.class.getResource("/slider/universe.mp4").toString());
  • 2. Re: Loading mp4 files
    colin-java Newbie
    Currently Being Moderated
    Thanks

    Thats basically what I already wrote though, I tried writing it as..
    media = new Media(MediaObject.class.getResource("/slider/universe.mp4").toString());
    Instead of this.getClass().getResource...
    But it does the same thing.

    So it works, but now the video won't repeat once it completes, unlike with the URI/URL way.
    I'm not sure how to do this, any thought(s)?
  • 3. Re: Loading mp4 files
    jsmith Guru
    Currently Being Moderated
    So it works, but now the video won't repeat once it completes, unlike with the URI/URL way.
    I'm not sure how to do this, any thought(s)?
    Publish a minimal, executable program so that somebody can replicate this - I don't get these kind of issues.
  • 4. Re: Loading mp4 files
    805536 Journeyer
    Currently Being Moderated
    The closing tag of the \
     tag is identical to the starting one. Don't place a '/' inside.
    
    The URI/URL technique you were using is directly accessing the media via _file protocol_ inside the source directory of your project, which of course will not work if you are trying to run this outside the project unless you similarly copy the folder over to the new location of the jar.
    
    Without more information, my best guess is that something is going wrong with _jar protocol_. [url http://docs.oracle.com/javafx/2/api/javafx/scene/media/MediaPlayer.html#setCycleCount(int)]It says here that cycleCount has no effect[/url] if the Media's duration is Duration.INDEFINITE. That should only occur in live streams, but maybe there's a bug. What is the result of calling Media.getDuration()?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 5. Re: Loading mp4 files
    colin-java Newbie
    Currently Being Moderated
    Thanks, I wrote a quick class that demonstrates the mp4 file is not getting repeated when built using 'class.getResource()'
    package main;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.scene.media.MediaView;
    import javafx.stage.Stage;
    
    
    public class Main extends Application
    {
      
        @Override
        public void start(Stage primaryStage)
        {
            MediaPlayer player = getMediaPlayer();
            MediaView view = new MediaView(player);
            view.setFitWidth(400);
            view.setFitHeight(300);
            
            
            StackPane root = new StackPane();
            root.getChildren().add(view);
            
            Scene scene = new Scene(root, 300, 250);
            
            primaryStage.setTitle("MediaPlayer");
            primaryStage.setScene(scene);
            primaryStage.show();
            
            player.play();
        }
    
     
        public static void main(String[] args)
        {
            launch(args);
        }
        
        
        public MediaPlayer getMediaPlayer()
        {
            // THE universe.mp4 SHOULD BE PLACED IN SAME PACKAGE (main) AS THIS CLASS
            Media media = new Media(Main.class.getResource("/main/universe.mp4").toString());
            
            MediaPlayer mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
            
            return mediaPlayer;
        }
    }
    Please try it and let it me know, thanks
  • 6. Re: Loading mp4 files
    colin-java Newbie
    Currently Being Moderated
    It turns out to be not indefinite as I expected, so I don't think the issue lies there.

    Thanks
  • 7. Re: Loading mp4 files
    colin-java Newbie
    Currently Being Moderated
    Just seeing if anyone has any more thoughts on this...
    Getting mp4 inside jar file to run, so it repeats, like it does with external mp4 files.

    Thanks
  • 8. Re: Loading mp4 files
    jsmith Guru
    Currently Being Moderated
    Thanks for publishing your test code colin. When I run a test I can replicate your error. Playback from the filesystem repeats. Playback from the jar resource, just plays once and doesn't repeat.

    I tried using your code, osx jdk7u11 and the test file from here: http://developer.longtailvideo.com/trac/browser/trunk/html5/test/files/bunny.mp4?rev=944

    I tried out aidreamers suggestion of monitoring the duration property with a changelistener and I noted that when the media started playing the duration was set correctly in both the play from jar and play from file cases.

    I advise you file a bug against the runtime at:
    http://javafx-jira.kenai.com
  • 9. Re: Loading mp4 files
    colin-java Newbie
    Currently Being Moderated
    Yeah, I think I will, I have to sign up for that thing though.

    Do you think its possible to get it to replay the movie once its complete.
    I'm not sure the setOnEndOfMedia(...) method actually works though when using class loader technique.

    Thanks
  • 10. Re: Loading mp4 files
    jsmith Guru
    Currently Being Moderated
    Do you think its possible to get it to replay the movie once its complete.
    Yeah, I'm sure it's possible to code up a workaround. I haven't tried writing anything though.
    I'm not sure the setOnEndOfMedia(...) method actually works though when using class loader technique.
    Yep, I don't think it does either.

    You will probably need to do something like work out the media length via a change listener, then setup a parallel timeline which will trigger another event in a KeyFrame after the media length duration has elapsed, and in the KeyFrame replace the dead MediaView with a new one. And keep doing it continuously to allow the looping to work. It's kind of clunky and my guess is that there will be some slight visual or audio glitches when doing this.

    Other options are to bypass the issue by hosting the media in a webserver and accessing via the http protocol, or on the filesystem and access via the file protocol, as it seems that it is only media which is accessed via the jar protcol which can't repeat.

Legend

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