Playing Video in JavaFX8

2019-07-24 07:42发布

问题:

I am new to JavaFX and Im currently working with an application that plays a video(.MP4) at the beginning of the application, however, I cant seem to make it work! Please tell me what's wrong with my code:

import java.io.File;
import javafx.geometry.Pos;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.text.Text;

public class NoConnection {

    private StackPane root;

    public NoConnection(){
        Media media = null;
        try{
            File video = new File("video.mp4");
            String url = video.toURI().toURL().toString();
            System.out.println("URL: "+url);
            media = new Media(url);
        }catch(Exception e){
        System.err.println(e.toString());
    }
        MediaPlayer player = new MediaPlayer(media);
        player.play();
        MediaView mediaView = new MediaView(player);

        root = new StackPane();
        root.setAlignment(Pos.CENTER);
        root.setStyle("-fx-background-color : white;");
        root.getChildren().add(mediaView);
    }

    public StackPane getLayout(){
        return root;
    }

}

By the way, Im running Windows XP!

Here's the complete error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/11461388.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/31501478.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/29531133.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

回答1:

This is all there is to do: Create a java class with this code:

public class VideoTest extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();

        MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
        MediaView mediaView = new MediaView(player);

        root.getChildren().add( mediaView);

        Scene scene = new Scene(root, 1024, 768);

        primaryStage.setScene(scene);
        primaryStage.show();


        player.play();

    }

}

and put the video.mp4 into the same folder. Start it. The video should play.

Btw I got a file not found exception using your code. And Windows XP isn't supported anymore. Neither by Oracle nor by Microsoft. If this simple code doesn't work, you got another problem.



回答2:

Here's the answer:

The FLV container is supported by the media stack on the platforms supported by the JavaFX SDK. A single movie encoded in this format works seamlessly on supported platforms. Standard FLV MIME settings are required on the server side to enable media streaming.

The MPEG-4 multimedia container is also supported on all operating systems supported by the JavaFX SDK. On the Mac OS X and Windows 7 platforms, playback will be functional without requiring additional software. However, the Linux operating system and versions of Windows older than Windows 7 require the installation of readily available third party software packages, as documented in the JavaFX System Requirements. AAC and H.264/AVC decoding have certain platform-dependent limitations, as described in the JavaFX Release Notes.

Decoding of some audio and video compression types relies on operating system-specific media engines. The JavaFX media framework does not attempt to handle all multimedia container formats and media encodings supported by these native engines. Instead, the framework attempts to provide equivalent and well-tested functionality across all platforms on which JavaFX is supported.