Example usage for javafx.scene.media MediaPlayer MediaPlayer

List of usage examples for javafx.scene.media MediaPlayer MediaPlayer

Introduction

In this page you can find the example usage for javafx.scene.media MediaPlayer MediaPlayer.

Prototype

public MediaPlayer(@NamedArg("media") Media media) 

Source Link

Document

Create a player for a specific media.

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    final URL resource = getClass().getResource("a.mp3");
    final Media media = new Media(resource.toString());
    final MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.play();// ww w  .j  ava 2s .c o m

    primaryStage.setTitle("Audio Player 1");
    primaryStage.setWidth(200);
    primaryStage.setHeight(200);
    primaryStage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    VBox box = new VBox();
    Scene scene = new Scene(box, 200, 200);
    stage.setScene(scene);//w  w w  .  j a va 2  s .  c  o  m

    Media media = new Media("video file URI");
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setAutoPlay(true);

    MediaView mediaView = new MediaView(mediaPlayer);
    ((VBox) scene.getRoot()).getChildren().add(mediaView);

    stage.show();
}

From source file:filesmodel.SoundModel.java

/** @return a MediaPlayer for the given source which will report any errors it encounters */
public MediaPlayer createPlayer(String mediaSource) {
    final Media media = new Media(mediaSource);
    final MediaPlayer player = new MediaPlayer(media);
    player.setOnError(new Runnable() {
        @Override//  w ww  . j  av a  2s . co m
        public void run() {
            System.out.println("Media error occurred: " + player.getError());
        }
    });

    return player;

}

From source file:net.gazeplay.commons.utils.games.Utils.java

public static void playSound(String ressource) {

    log.debug("Try to play " + ressource);

    URL url = ClassLoader.getSystemResource(ressource);

    String path = null;// w  w w.  ja  v a  2s  . c o  m

    if (url == null) {
        final File file = new File(ressource);
        log.debug("using file");
        if (!file.exists()) {
            log.warn("file doesn't exist : {}", ressource);
        }
        path = file.toURI().toString();
    } else {
        log.debug("using url");
        path = url.toString();
    }

    log.debug("path " + path);
    if (sxmp != null)
        sxmp.stop();
    try {
        Media media = new Media(path);
        sxmp = new MediaPlayer(media);
        final Configuration configuration = Configuration.getInstance();
        sxmp.setVolume(configuration.getEffectsVolume());
        sxmp.volumeProperty().bind(configuration.getEffectsVolumeProperty());
        sxmp.play();
    } catch (Exception e) {
        log.error("Exception", e);
    }
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    String workingDir = System.getProperty("user.dir");
    final File f = new File(workingDir, "../media/omgrobots.flv");

    final Media m = new Media(f.toURI().toString());
    final MediaPlayer mp = new MediaPlayer(m);
    final MediaView mv = new MediaView(mp);

    final DoubleProperty width = mv.fitWidthProperty();
    final DoubleProperty height = mv.fitHeightProperty();

    width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
    height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));

    mv.setPreserveRatio(true);/*from  ww  w .  j  a v a  2  s .c  o  m*/

    StackPane root = new StackPane();
    root.getChildren().add(mv);

    final Scene scene = new Scene(root, 960, 540);
    scene.setFill(Color.BLACK);

    primaryStage.setScene(scene);
    primaryStage.setTitle("Full Screen Video Player");
    primaryStage.setFullScreen(true);
    primaryStage.show();

    mp.play();
}

From source file:projavafx.videoplayer3.VideoPlayer3.java

@Override
public void start(Stage primaryStage) {
    final Label message = new Label("I \u2764 Robots");
    message.setVisible(false);//from  w  w w  .  j  a  v a 2  s.com

    String workingDir = System.getProperty("user.dir");
    final File f = new File(workingDir, "../media/omgrobots.flv");

    final Media m = new Media(f.toURI().toString());
    m.getMarkers().put("Split", Duration.millis(3000));
    m.getMarkers().put("Join", Duration.millis(9000));

    final MediaPlayer mp = new MediaPlayer(m);

    final MediaView mv1 = new MediaView(mp);
    mv1.setViewport(new Rectangle2D(0, 0, 960 / 2, 540));
    StackPane.setAlignment(mv1, Pos.CENTER_LEFT);

    final MediaView mv2 = new MediaView(mp);
    mv2.setViewport(new Rectangle2D(960 / 2, 0, 960 / 2, 540));
    StackPane.setAlignment(mv2, Pos.CENTER_RIGHT);

    StackPane root = new StackPane();
    root.getChildren().addAll(message, mv1, mv2);
    root.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            mp.seek(Duration.ZERO);
            message.setVisible(false);
        }
    });

    final Scene scene = new Scene(root, 960, 540);
    final URL stylesheet = getClass().getResource("media.css");
    scene.getStylesheets().add(stylesheet.toString());

    primaryStage.setScene(scene);
    primaryStage.setTitle("Video Player 3");
    primaryStage.show();

    mp.setOnMarker(new EventHandler<MediaMarkerEvent>() {

        @Override
        public void handle(final MediaMarkerEvent event) {
            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    if (event.getMarker().getKey().equals("Split")) {
                        message.setVisible(true);
                        buildSplitTransition(mv1, mv2).play();
                    } else {
                        buildJoinTransition(mv1, mv2).play();
                    }
                }

            });
        }
    });
    mp.play();
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    final Label markerText = new Label();
    StackPane.setAlignment(markerText, Pos.TOP_CENTER);

    String workingDir = System.getProperty("user.dir");
    final File f = new File(workingDir, "../media/omgrobots.flv");

    final Media m = new Media(f.toURI().toString());

    final ObservableMap<String, Duration> markers = m.getMarkers();
    markers.put("Robot Finds Wall", Duration.millis(3100));
    markers.put("Then Finds the Green Line", Duration.millis(5600));
    markers.put("Robot Grabs Sled", Duration.millis(8000));
    markers.put("And Heads for Home", Duration.millis(11500));

    final MediaPlayer mp = new MediaPlayer(m);
    mp.setOnMarker(new EventHandler<MediaMarkerEvent>() {
        @Override//from   ww w.ja v a  2  s.  co  m
        public void handle(final MediaMarkerEvent event) {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    markerText.setText(event.getMarker().getKey());
                }
            });
        }
    });

    final MediaView mv = new MediaView(mp);

    final StackPane root = new StackPane();
    root.getChildren().addAll(mv, markerText);
    root.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            mp.seek(Duration.ZERO);
            markerText.setText("");
        }
    });

    final Scene scene = new Scene(root, 960, 540);
    final URL stylesheet = getClass().getResource("media.css");
    scene.getStylesheets().add(stylesheet.toString());

    primaryStage.setScene(scene);
    primaryStage.setTitle("Video Player 2");
    primaryStage.show();

    mp.play();
}

From source file:com.tesshu.subsonic.client.sample4_music_andmovie.StreamPlayApplication.java

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

    Search2Controller search2 = context.getBean(Search2Controller.class);

    StreamController streamController = context.getBean(StreamController.class);

    SuccessObserver callback = context.getBean(SuccessObserver.class);

    SearchResult2 result2 = search2.get("t", null, null, null, null, 1, null, null);

    Child song = result2.getSongs().get(0);

    final IRequestUriObserver uriCallBack = (subject, uri) -> {
        uriStr = uri.toString();/* ww w  .  j  ava2  s.com*/
    };

    streamController.stream(song, 128, // maxBitRate
            format, // format
            null, // timeOffset
            null, // size(Do not use it for video)
            true, // estimateContentLength
            null, // converted(Do not use it for video)
            null, // streamCallback
            uriCallBack, callback);

    Group root = new Group();
    Scene scene = new Scene(root, 640, 480);
    Media media = new Media(uriStr);
    media.errorProperty().addListener((observable, old, cur) -> {
        LOG.info(cur + " : " + uriStr);
    });

    MediaPlayer player = new MediaPlayer(media);
    player.statusProperty().addListener((observable, old, cur) -> {
        LOG.info(cur + " : " + uriStr);
    });

    MediaView view = new MediaView(player);
    ((Group) scene.getRoot()).getChildren().add(view);
    stage.setScene(scene);
    stage.show();
    player.play();

}

From source file:benedict.zhang.addon.soundmanager.controller.SoundManagerConfigureController.java

@Override
public void handle(WindowEvent event) {
    Sound dbSound = null;/*  ww  w .j  ava2 s  .  c  o  m*/
    if (WindowEvent.WINDOW_SHOWN.equals(event.getEventType())) {
        try {
            dbSound = SoundDataProxy.getInstance().getDataSound(ApplicationUIConstants.SOUND_INFO,
                    Boolean.TRUE);
            BeanUtils.copyProperties(sound, dbSound);
            Media media = new Media(new File(sound.getSoundPath()).toURI().toURL().toString());
            if (media != null) {
                mediaPlayer = new MediaPlayer(media);
                mediaPlayer.setOnPlaying(() -> {
                    btnSoundPlayPause.setText("Pause");
                    this.sound.setTotalDuration(mediaPlayer.getTotalDuration());
                });
                mediaPlayer.setOnPaused(() -> {
                    btnSoundPlayPause.setText("Play");
                });
                mediaPlayer.setOnStopped(() -> {
                    btnSoundPlayPause.setText("Play");
                });
                mediaPlayer.setOnEndOfMedia(() -> {
                    btnSoundPlayPause.setText("Play");
                    mediaPlayer.stop();
                });
                configMediaView.setMediaPlayer(mediaPlayer);
                String mediaInfo = sound.getSoundPath();
                soundFileName.setText(mediaInfo);
                mediaPlayer.play();
            }
        } catch (MalformedURLException ex) {
            this.dialog.hide();
        } catch (IllegalAccessException ex) {
            this.dialog.hide();
        } catch (InvocationTargetException ex) {
            this.dialog.hide();
        } catch (MediaException me) {
            if (dbSound != null) {
                PersistenceManager.getInstance().removeSound(dbSound);
            }
            this.dialog.hide();

        }
    }

    if (WindowEvent.WINDOW_HIDING.equals(event.getEventType())) {
        if (this.mediaPlayer != null) {
            this.mediaPlayer.stop();
        }
        SoundDataProxy.getInstance().storeSound(ApplicationUIConstants.SOUND_INFO, oriSound);
    }
}

From source file:com.tesshu.subsonic.client.sample4_music_andmovie.StreamPlayMovieApplication.java

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

    Search2Controller search2 = context.getBean(Search2Controller.class);

    StreamController streamController = context.getBean(StreamController.class);

    SuccessObserver callback = context.getBean(SuccessObserver.class);

    SearchResult2 result2 = search2.get("CORPSE BRIDE", // query, required = true
            0, // artistCount, required = false
            null, // artistOffset, required = false
            0, // albumCount, required = false
            null, // albumOffset, required = false
            10, // songCount, required = false
            null, // songOffset, required = false
            null // musicFolderId, required = false
    );/*from   ww  w. j a v a 2s  . co m*/

    Child movie = result2.getSongs().stream().filter(child -> MediaType.VIDEO == child.getType())
            .filter(child -> format.equals(child.getSuffix())).collect(Collectors.toSet()).iterator().next();

    LOG.info(ToStringBuilder.reflectionToString(movie, ToStringStyle.MULTI_LINE_STYLE));

    // not valid?(Perhaps, I have not done convert setting on the server side)
    @SuppressWarnings("unused")
    String size = Integer.toString(movie.getOriginalWidth()) + "x"
            + Integer.toString(movie.getOriginalHeight());

    final IRequestUriObserver uriCallBack = (subject, uri) -> {
        uriStr = uri.toString();
    };

    streamController.stream(

            movie, // id
            null, // maxBitRate
            format, // format
            null, // timeOffset
            null, // size
            true, // estimateContentLength
            false, // converted
            null, // streamCallback
            uriCallBack, callback);

    Group root = new Group();
    Scene scene = new Scene(root, movie.getOriginalWidth(), movie.getOriginalHeight());
    Media media = new Media(uriStr);
    media.errorProperty().addListener((observable, old, cur) -> {
        LOG.info(cur + " : " + uriStr);
    });

    MediaPlayer player = new MediaPlayer(media);
    player.statusProperty().addListener((observable, old, cur) -> {
        LOG.info(cur + " : " + uriStr);
    });

    MediaView view = new MediaView(player);
    ((Group) scene.getRoot()).getChildren().add(view);
    stage.setScene(scene);
    stage.show();
    player.play();

}