-
-
Save carterpeel/8ac39c40c384a0bbd9d339f1b1222e62 to your computer and use it in GitHub Desktop.
Play .mp4 video using Java MediaPlayer for 5 seconds. After that, the video will be closed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.beans.binding.Bindings; | |
import javafx.beans.property.DoubleProperty; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.media.Media; | |
import javafx.scene.media.MediaView; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
import javafx.util.Duration; | |
import java.io.File; | |
public class Video extends Application{ | |
private String Dir = System.getProperty("user.dir"); | |
public static void main(String[] args) throws Exception{ | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
//goes to user Directory | |
File f = new File(Dir, "content/test.mp4"); | |
//Converts media to string URL | |
Media media = new Media(f.toURI().toURL().toString()); | |
javafx.scene.media.MediaPlayer player = new javafx.scene.media.MediaPlayer(media); | |
MediaView viewer = new MediaView(player); | |
//change width and height to fit video | |
DoubleProperty width = viewer.fitWidthProperty(); | |
DoubleProperty height = viewer.fitHeightProperty(); | |
width.bind(Bindings.selectDouble(viewer.sceneProperty(), "width")); | |
height.bind(Bindings.selectDouble(viewer.sceneProperty(), "height")); | |
viewer.setPreserveRatio(true); | |
StackPane root = new StackPane(); | |
root.getChildren().add(viewer); | |
//set the Scene | |
Scene scenes = new Scene(root, 500, 500, Color.BLACK); | |
stage.setScene(scenes); | |
stage.setTitle("test"); | |
stage.show(); | |
player.play(); | |
player.setStopTime(Duration.millis(5000.0)); | |
player.setOnEndOfMedia(new Runnable() { | |
@Override | |
public void run() { | |
player.stop(); | |
stage.close(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment