2017-07-11 4 views
0

Ich habe eine Frage über Java bezogen auf die Klasse MediaPlayer, die zum Abspielen eines Einführungsvideos verwendet wird. Der Punkt ist, dass das Video bei der Ausführung meiner Anwendung manchmal korrekt abgespielt wird und manchmal nicht, meistens nicht. Mit dem Hinweis, dass es nicht richtig abgespielt wird, verweise ich darauf, dass das Audio zwar abgespielt wird aber das Bild nicht. So kann ich schließen, dass MediaPlayer nicht richtig funktioniert. HierMediaPlayer funktioniert nicht richtig in Java

ist der Code meiner Anwendung:

/** 
* Main class of the application. 
*/ 
public class Main{ 

    // Define the variable for the window of the game. 
    public static JFrame window; 

    // Define the variable for the introductory video. 
    public static MediaPlayer video; 

    /** 
    * Main function of the application. 
    */ 
    public static void main(String[] args){ 

    // Prevent the JavaFX toolkit from closing. 
    Platform.setImplicitExit(false); 

    // Create the window of the game. 
    window = new JFrame(); 

    // Set the title. 
    window.setTitle("Chip"); 

    // Set the resolution as 1920 x 1280. 
    window.setSize(1926,1343); 

    // Set the location as in the middle of the screen. 
    window.setLocationRelativeTo(null); 

    // Set the operation when the window closes. 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // Disable the maximization and resizable mode. 
    window.setResizable(false); 

    // Show the window. 
    window.setVisible(true); 

    // Show the introductory video. 
    showVideo(); 

    // Pause the execution of the application for 30 seconds (duration of the introductory video). 
    try{ 
     Thread.sleep(30000); 
    }catch (InterruptedException interruptedException){ 
     interruptedException.printStackTrace(); 
    } 

    } 


    /** 
    * Shows the introductory video. 
    */ 
    public static void showVideo(){ 

    // Create the video panel and the JavaFX panel. 
    JPanel panelVideo = new JPanel(); 
    JFXPanel panelJavaFX = new JFXPanel(); 

    // Set the size of the video panel as the resolution of the introductory video (1920 x 1080). 
    panelVideo.setSize(1920,1080); 

    // Set the location of the video panel as in the middle of the window of the game. 
    int coordinateX = (window.getWidth() - panelVideo.getWidth() - window.getInsets().left - window.getInsets().right)/2; 
    int coordinateY = (window.getHeight() - panelVideo.getHeight() - window.getInsets().top - window.getInsets().bottom)/2; 
    panelVideo.setLocation(coordinateX,coordinateY); 

    // Define the video file. 
    String filename = "./media/video/introduction.mp4"; 
    video = new MediaPlayer(new Media(new File(filename).toURI().toString())); 

    // Add the video to the JavaFX panel. 
    panelJavaFX.setScene(new Scene(new Group(new MediaView(video)))); 

    // Add the JavaFX panel to the video panel. 
    panelVideo.add(panelJavaFX); 

    // Add the video panel to the window of the game. 
    window.getContentPane().setLayout(null); 
    window.add(panelVideo); 

    // Play the video. 
    video.play(); 

    } 

} 
+0

Verwenden Sie JavaFX? Wenn ja, würde ich empfehlen, dass Sie MediaView verwenden und nicht ein Panel, um Ihren MediaPlayer zu behandeln –

+0

Ja, ich benutze JavaFX. Wie würde der Code dann aussehen? Ich habe keine Ahnung. –

+0

Entschuldigung, mein Fehler, Sie benutzen es. –

Antwort

0

Stellen Sie Ihre video.play(); Methode direkt nach der Initialisierung Ihres Mediaplayer, zumindest für mich, das schien es zu beheben, das heißt:

video = new MediaPlayer(new Media(new File(filename).toURI().toString())); 
video.play(); 
0

Die richtige Antwort auf diese Frage lautet:

Ich habe die Variable panelJavaFX (JFXPanel) direkt auf die Variable 012 hinzugefügt(JFrame), so habe ich schließlich die Zwischenvariable panelVideo (JPanel) nicht verwendet.

Verwandte Themen