user1538070
user1538070

Reputation: 43

How to run a javaFX MediaPlayer in swing?

I've made a simple Media Player for an application I'm working on, the problem is that I thought that you could simply integrate JavaFX into Swing. Which is not the case. I have been searching for a solution to this problem and tried to use this website: http://docs.oracle.com/javafx/2/swing/jfxpub-swing.htm The problem is that even though I have the website that explains how to put the code together, I still don't understand how. Here is the mediaplayer and I plan to integrate it into my Swing code, so that I can call the media player when a button is clicked. Here is all my code for the media player and if anyone can share some light on how to integrate it into my Swing code i.e my GUI, I would probably have to kiss you through the computer.

public class Player extends Application{

private boolean atEndOfMedia = false;
private final boolean repeat = false;
private boolean stopRequested = false;
private Duration duration;
private Label playTime;
private Slider volumeSlider;

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

    stage.setTitle("Movie Player");//set title
    Group root = new Group();//Group for buttons etc

    final Media media = new Media("file:///Users/Paul/Downloads/InBruges.mp4");
    final MediaPlayer playa = new MediaPlayer(media);
    MediaView view = new MediaView(playa);

    //Slide in and out and what causes that.
    final Timeline slideIn = new Timeline();
    final Timeline slideOut = new Timeline();
    root.setOnMouseEntered(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>()              { 
        @Override
        public void handle(MouseEvent t) {
            slideIn.play();
        }
    });

    root.setOnMouseExited(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>() {
        @Override
        public void handle(MouseEvent t) {
            slideOut.play();
        }
    });

    final VBox vbox = new VBox();
    final Slider slider = new Slider();
    final Button playButton  = new Button("|>");

    root.getChildren().add(view);
    root.getChildren().add(vbox);
    vbox.getChildren().add(slider);
    vbox.getChildren().add(playButton);
    vbox.setAlignment(Pos.CENTER);

    Scene scene = new Scene(root, 400, 400, Color.BLACK);
    stage.setScene(scene);
    stage.show();

    // Play/Pause Button
   playButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
    public void handle(ActionEvent e) {
    Status status = playa.getStatus();

    if (status == Status.UNKNOWN  || status == Status.HALTED)
    { 
       // don't do anything in these states
       return;
    }

      if ( status == Status.PAUSED
         || status == Status.READY
         || status == Status.STOPPED)
      {
         // rewind the movie if we're sitting at the end
         if (atEndOfMedia) {
            playa.seek(playa.getStartTime());
            atEndOfMedia = false;
         }
         playa.play();
         } else {
           playa.pause();
         }
     }
 });

   //Listeners and Shit for Play Button
    playa.setOnPlaying(new Runnable() {
        @Override
        public void run() {
            if (stopRequested) {
                playa.pause();
                stopRequested = false;
            } else {
                playButton.setText("||");
            }
        }
    });

    playa.setOnPaused(new Runnable() {
        @Override
        public void run() {
            playButton.setText(">");
        }
    });

    playa.play();
    playa.setOnReady(new Runnable() {

        @Override    
        public void run(){
        int v = playa.getMedia().getWidth();
        int h = playa.getMedia().getHeight();

        stage.setMinWidth(v);
        stage.setMinHeight(h);

        vbox.setMinSize(v, 100);
        vbox.setTranslateY(h-50);

        //slider and graphical slide in/out
        slider.setMin(0.0);
        slider.setValue(0.0);
        slider.setMax(playa.getTotalDuration().toSeconds());

        slideOut.getKeyFrames().addAll(
                new KeyFrame(new Duration(0),
                      new KeyValue(vbox.translateYProperty(), h-100),
                      new KeyValue(vbox.opacityProperty(), 0.9)
                            ),
                new KeyFrame(new Duration(300),
                      new KeyValue(vbox.translateYProperty(), h),
                      new KeyValue(vbox.opacityProperty(), 0.0)
          )
          );
        slideIn.getKeyFrames().addAll(
                new KeyFrame(new Duration(0),
                    new KeyValue(vbox.translateYProperty(), h),
                    new KeyValue(vbox.opacityProperty(), 0.0) 
                            ),
                new KeyFrame(new Duration(300),
                        new KeyValue(vbox.translateYProperty(), h-100),
                        new KeyValue(vbox.opacityProperty(), 0.9)
          )
          );
    }

});

    //Slider being current and ability to click on slider. 
playa.currentTimeProperty().addListener(new ChangeListener<Duration>(){
    @Override
    public void changed(ObservableValue<? extends Duration> observableValue, Duration   duration, Duration current){
       slider.setValue(current.toSeconds());
   }
  });
   slider.setOnMouseClicked(new javafx.event.EventHandler<javafx.scene.input.MouseEvent>()   {

        @Override
        public void handle(javafx.scene.input.MouseEvent t) {
            playa.seek(Duration.seconds(slider.getValue()));
        }
});
}

Upvotes: 2

Views: 3923

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34478

Use JFXPanel:

 public class Test {

     private static void initAndShowGUI() {
         // This method is invoked on Swing thread
         JFrame frame = new JFrame("FX");
         final JFXPanel fxPanel = new JFXPanel();
         frame.add(fxPanel);
         frame.setVisible(true);

         Platform.runLater(new Runnable() {
             @Override
             public void run() {
                 initFX(fxPanel);
             }
         });
     }

     private static void initFX(JFXPanel fxPanel) {
         // This method is invoked on JavaFX thread
         Scene scene = createScene();
         fxPanel.setScene(scene);
     }

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 initAndShowGUI();
             }
         });
     }
 }

where method createScene() is start(final Stage stage) from your code. Just instead of putting scene to stage you return it.

Upvotes: 2

Related Questions