Error in Playing .mp4 video using Java Springboot

52 Views Asked by At

I am having issues playing .mp4 video in Java Springboot. The exception I am getting is "MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!".

Here is my code

import javafx.application.Application;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.embed.swing.JFXPanel;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

@Controller
public class VideoPlayer{

    @GetMapping("/playVideo")
    public String playVideo(Model model) {
        try {
            String videoFile = "src/main/resources/movies/IMG_0055.mp4"; // Replace with the actual path to your MOV file

            // Initialize JavaFX toolkit (needed for MediaPlayer)
            Toolkit.getDefaultToolkit();

            // Create a media object
            Media media = new Media(new URL("file:" + videoFile).toExternalForm());
            // Create a media player
            MediaPlayer mediaPlayer = new MediaPlayer(media);
            mediaPlayer.setAutoPlay(true);

            // Create a media view
            MediaView mediaView = new MediaView(mediaPlayer);

            // Set up the JavaFXPanel for video playback
            JFXPanel fxPanel = new JFXPanel();
            fxPanel.setScene(new javafx.scene.Scene(new javafx.scene.layout.BorderPane(mediaView)));

            // Set up the JFrame
            JFrame frame = new JFrame("Video Player");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setContentPane(fxPanel);

            // Display the JFrame
            frame.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error", "Error playing video: " + e.getMessage());
        }

        return "video";
    }

}

I imported Media, Media View and Media Player. I did not import javafx.media library since it is no longer used. I instead imported javafx.scene.media.Media. Could you help me why this may be an error.

0

There are 0 best solutions below