JavaFX Media - JavaFX Video and Audio Support
Jakob Jenkov |
The JavaFX media support, meaning JavaFX video and audio support, is provided by the JavaFX media classes Media, MediaPlayer, MediaView and AudioClip. In this tutorial I will explain how to use these classes.
JavaFX Video Example
JavaFX video support is provided by the Media, MediaPlayer and MediaView classes. Here is an example of playing an MP4 video file within a JavaFX application:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaView; import javafx.stage.Stage; import java.io.File; import java.net.MalformedURLException; public class MediaExample extends Application { public static void main(String[] args) { launch(args); } public void start(Stage primaryStage) throws MalformedURLException { File mediaFile = new File("assets/media/Golden-48569.mp4"); Media media = new Media(mediaFile.toURI().toURL().toString()); MediaPlayer mediaPlayer = new MediaPlayer(media); MediaView mediaView = new MediaView(mediaPlayer); Scene scene = new Scene(new Pane(mediaView), 1024, 800); primaryStage.setScene(scene); primaryStage.show(); mediaPlayer.play(); } }
Next: JavaFX Canvas
Tweet | |
Jakob Jenkov |