Achala Weerasooriya
Achala Weerasooriya

Reputation: 215

javafx mediaview only the audio is playing

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>

Upvotes: 0

Views: 2334

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36792

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();
} 

Upvotes: 3

Related Questions