JavaFx之播放MP4(二十七)

JavaFx之播放MP4(二十七)

JavaFX 视频和音频支持,由 JavaFX 媒体类 Media、MediaPlayer、MediaView 和 AudioClip 提供。

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;
/**
 * @author lingkang
 * Created by 2022/8/15
 */
public class MediaExample extends Application {

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

    public void start(Stage primaryStage) throws MalformedURLException {
        File mediaFile = new File("C:\\快剪辑视频\\2.5召唤10分钟刮痧寂静城【冒险级】.mp4");
        Media media = new Media(mediaFile.toURI().toURL().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        MediaView mediaView = new MediaView(mediaPlayer);
        mediaView.setFitWidth(1024);
        mediaView.setFitHeight(800);
        
        Scene scene = new Scene(new Pane(mediaView), 1024, 800);
        primaryStage.setScene(scene);
        primaryStage.show();

        mediaPlayer.play();
    }
}

在这里插入图片描述

posted @ 2022-09-16 00:07  凌康  阅读(121)  评论(0编辑  收藏  举报