简体   繁体   English

如何播放 mp4 JavaFx MediaPlayer

[英]How to play mp4 JavaFx MediaPlayer

I have a mediaPlayer application with javafx, I can send one MediaSource from one controller to another, but the mp4 file does not play on the controller.我有一个带有 javafx 的 mediaPlayer 应用程序,我可以将一个 MediaSource 从一个控制器发送到另一个控制器,但 mp4 文件无法在控制器上播放。 There are no error or whatsoever, it just stays blank.没有错误或任何其他内容,它只是保持空白。

I tried to System.Out.print the source of the send media, and it is correct, if I click on the source windows 10 plays the video.我尝试 System.Out.print 发送媒体的来源,这是正确的,如果我点击源 Windows 10 播放视频。 Can maybe someone look over it or is there a limit on what javafx can play and what not?也许有人可以查看它,或者对 javafx 可以播放的内容和不能播放的内容有限制吗?

Controller and Method to send a mediaPath to the player.向播放器发送 mediaPath 的控制器和方法。

public void videoLoop() {
        File videoDirectory = new File("src\\ressources\\videos");
        File[] listOfFiles = videoDirectory.listFiles();
        File imgDirectory = new File("src\\ressources\\thumbnails");
        File[] imglistOfFiles = imgDirectory.listFiles();

        int i = 0;
        int j = 0;

        for (i = 0; i <=  listOfFiles.length - 1; i++) {

            for (j = 0; j <= imglistOfFiles.length -1 ; j++) {
                if (listOfFiles[i].getName().replaceAll("(\\..+)$","").equals
                        (imglistOfFiles[j].getName().replaceAll("(\\..+)$",""))) {
                        ImageView imgView = new ImageView();
                        Image img = new Image("file:" + imglistOfFiles[j].getAbsolutePath());
                        imgView.setImage(img);
                    int finalJ1 = j;
                    mediaPath ="src\\ressources\\videos\\" + imglistOfFiles[finalJ1].getName().replaceAll("(\\..+)$","")  + ".mp4";
                    imgView.setOnMouseClicked(event -> {

     selectionMedia = new Media(Paths.get(mediaPath).toUri().toString());
                        var window = utilities.viewSwitcher.getWindow(event);
                        var loader = new FXMLLoader();
                        loader.setLocation(videoSelectionController.class.getResource("/view/videoPlayerView.fxml"));
                        VideoPlayerController videoPlayerController = new VideoPlayerController();
                        videoPlayerController.setMedia(selectionMedia);
                        videoPlayerController.videoPlay();

                        try {
                            window.setScene(new Scene(loader.load()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                        window.sizeToScene();

                    });
                    buttonBox.getChildren().add(imgView);


                }

Method to play the video播放视频的方法

public void videoPlay(){

        var loader = new FXMLLoader(VideoSelectionController.class.getResource("/view/videoSelection.fxml"));
        try {
            loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        VideoSelectionController controller = loader.getController();
        videoPlayer = new MediaView();
        MediaPlayer player = new MediaPlayer(this.media);
        videoPlayer.setMediaPlayer(player);
        player.play();

    }

Hello Jizang Depending on which version of JavaFX you are using support for media formats can vary. Hello Jizang 根据您使用的 JavaFX 版本,对媒体格式的支持可能会有所不同。 If you are using JavaFX 8 then you are unable to use MP4 files with the MediaPlayer.如果您使用的是 JavaFX 8,那么您将无法通过 MediaPlayer 使用 MP4 文件。 You will instead have to use one of the following formats for JavaFX 8您将不得不为 JavaFX 8 使用以下格式之一

"Audio: MP3; AIFF containing uncompressed PCM; WAV containing uncompressed PCM; MPEG-4 multimedia container with Advanced Audio Coding (AAC) audio Video: FLV containing VP6 video and MP3 audio; MPEG-4 multimedia container with H.264/AVC (Advanced Video Coding) video compression " -Direct quote from https://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm accessed 10/14/2020 “音频:MP3;包含未压缩 PCM 的 AIFF;包含未压缩 PCM 的 WAV;具有高级音频编码 (AAC) 音频的 MPEG-4 多媒体容器视频:包含 VP6 视频和 MP3 音频的 FLV;具有 H.264/AVC 的 MPEG-4 多媒体容器(高级视频编码)视频压缩“ - 直接引用自https://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm访问 10/14/2020

I was unable to find information for the mediaplayer on later versions of JavaFX More info on the topic can be found here我无法在更高版本的 JavaFX 上找到有关媒体播放器的信息,可以在此处找到有关该主题的更多信息

-Coder5986 -Coder5986

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM