javafx mediaview only the audio is playing

瘦欲@ 提交于 2020-01-11 13:17:27

问题


Im trying to embed a video in a website to a mediaview of a simple javafx application. Ive got a sample code which I used as my javafx code. It opens the scene and plays the audio but wont play the video. How can I make it play the audio? Im using netbeans IDE 8.0.2,JavaFx 8 and scene builder 2.0 The Code ive tried is below. Thanx in advanced.

@FXML MediaView mdv;
Media media;
public static MediaPlayer mpl;
@Override
public void initialize(URL url, ResourceBundle rb) {
    media=new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
    mpl=new MediaPlayer(media);
    mpl.setAutoPlay(true);
    mdv=new MediaView(mpl);
    mpl.play();
    mpl.setOnError(new Runnable() {
        @Override public void run() {
            System.out.println("Current Error: " + mpl.getError());
        }
    });
}   

This is how I load the children to the stage

public class Production extends Application {

@Override
public void start(Stage stage) throws Exception {

    Parent root = FXMLLoader.load(getClass().getResource("Vdo.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

the FXML Ive got after creating the GUI from scene builder is below

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.media.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="production.VdoController">
   <children>
      <MediaView fx:id="mdv" fitHeight="300.0" fitWidth="300.0" layoutX="125.0"  layoutY="85.0" />
      <Button layoutX="235.0" layoutY="51.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

回答1:


You are re-initializing the MediaView that has been initialized by the FXMLoader. Never do this, because you will loose the reference to the original node.

You should just set the MediaPlayer to the MediaView instead of re-initializing it by using the setMediaPlayer().

@Override
public void initialize(URL url, ResourceBundle rb) {
    media=new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
    mpl=new MediaPlayer(media);
    mpl.setAutoPlay(true);
    mdv.setMediaPlayer(mpl);
    mpl.play();
} 


来源:https://stackoverflow.com/questions/32805362/javafx-mediaview-only-the-audio-is-playing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!