Saturday, March 10, 2012

JavaFX 2.0: Example of MediaPlay

package javafx_media;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_VideoPlayer extends Application {
    
    private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
     private static String arg1;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com/");
        
        Group root = new Group();
        Scene scene = new Scene(root, 540, 210);

        // create media player
        Media media = new Media((arg1 != null) ? arg1 : MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        
        // create mediaView and add media player to the viewer
        MediaView mediaView = new MediaView(mediaPlayer);
        ((Group)scene.getRoot()).getChildren().add(mediaView);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}



Actually, it's a example preovided from Oracle JavaFX 2.0 Doc here.

Related:
- Embed JavaFX inside Swing JFrame


5 comments:

  1. hi really god example, ihave a question. how can i call this player from a jframe o jpanel?

    ReplyDelete
  2. really thanks sorry for the late in the response

    ReplyDelete
  3. Hey, everytime I try to run your example, I've got a black scene.

    The MediaPlayer.Status is still "UNKNOWN".
    Any clues what i've might done wrong?

    ReplyDelete
    Replies
    1. Have you installed your lib format ? if not, please install it
      dirac-libs-1.0.2-9.fc18.i686.rpm

      libavcodec52-0.7.15-32.fc18.i686.rpm

      libavformat52-0.7.15-32.fc18.i686.rpm

      libavutil50-0.7.15-32.fc18.i686.rpm

      libxavs1-0.1.51-2.fc18.i686.rpm

      Once installed, javafx plays music like a charm.

      Delete