Example usage for javafx.scene.media MediaPlayer getStatus

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

Introduction

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

Prototype

public final Status getStatus() 

Source Link

Document

Retrieves the current player status.

Usage

From source file:com.tesshu.subsonic.client.sample4_music_andmovie.StreamDownloadAndPlayWithThreadApplication.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("e", null, null, null, null, 1, null, null);

    List<Child> songs = result2.getSongs();

    File tmpDirectory = new File(tmpPath);
    tmpDirectory.mkdir();//from w w  w.ja  v a  2 s  .c  om

    int maxBitRate = 256;

    Child song = songs.get(0);

    new Thread(new Runnable() {
        public void run() {
            try {

                streamController.stream(song, maxBitRate, format, null, null, null, null,

                        (subject, inputStream, contentLength) -> {

                            File dir = new File(
                                    tmpPath + "/" + song.getPath().replaceAll("([^/]+?)?$", StringUtils.EMPTY));
                            dir.mkdirs();

                            file = new File(tmpPath + "/"
                                    + song.getPath().replaceAll("([^.]+?)?$", StringUtils.EMPTY) + format);

                            try {

                                FileOutputStream fos = new FileOutputStream(file);
                                BufferedInputStream reader = new BufferedInputStream(inputStream);

                                byte buf[] = new byte[256];
                                int len;
                                while ((len = reader.read(buf)) != -1) {
                                    fos.write(buf, 0, len);
                                }
                                fos.flush();
                                fos.close();
                                reader.close();
                                inputStream.close();

                                LOG.info("download finished");

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }, callback);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

    LOG.info("download thread start");

    new Thread(new Runnable() {
        public void run() {
            while (file == null || file.getPath() == null) {
                LOG.info("wait file writing.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                String path = Paths.get(file.getPath()).toUri().toString();
                Group root = new Group();
                Scene scene = new Scene(root, 640, 480);
                Media media = new Media(path);
                MediaPlayer player = new MediaPlayer(media);
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            while (MediaPlayer.Status.READY != player.getStatus()) {
                                LOG.info(player.getStatus() + " : " + path);
                                LOG.info(media.errorProperty());
                                Thread.sleep(1000);
                                if (MediaPlayer.Status.PLAYING == player.getStatus()) {
                                    LOG.info(player.getStatus() + " : " + path);
                                    break;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                MediaView view = new MediaView(player);
                ((Group) scene.getRoot()).getChildren().add(view);

                Platform.runLater(() -> {
                    stage.setScene(scene);
                    stage.show();
                    player.play();
                });

            }
        }
    }).start();

}

From source file:AudioPlayer3.java

private void seekAndUpdatePosition(Duration duration) {
    final MediaPlayer mediaPlayer = songModel.getMediaPlayer();

    if (mediaPlayer.getStatus() == Status.STOPPED) {
        mediaPlayer.pause();//from  ww  w  .j  a v a2 s .  c  o m
    }

    mediaPlayer.seek(duration);

    if (mediaPlayer.getStatus() != Status.PLAYING) {
        updatePositionSlider(duration);
    }
}

From source file:AudioPlayer3.java

private Button createPlayPauseButton() {
    URL url = getClass().getResource("resources/pause.png");
    pauseImg = new Image(url.toString());

    url = getClass().getResource("resources/play.png");
    playImg = new Image(url.toString());

    playPauseIcon = new ImageView(playImg);

    final Button playPauseButton = new Button(null, playPauseIcon);
    playPauseButton.setId("playPauseButton");
    playPauseButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override/*from  www . j  a  v  a2  s.co  m*/
        public void handle(ActionEvent arg0) {
            final MediaPlayer mediaPlayer = songModel.getMediaPlayer();
            if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
                mediaPlayer.pause();
            } else {
                mediaPlayer.play();
            }
        }
    });
    return playPauseButton;
}

From source file:MediaControl.java

public MediaControl(final MediaPlayer mp) {
    this.mp = mp;
    setStyle("-fx-background-color: #bfc2c7;");
    mediaView = new MediaView(mp);
    Pane mvPane = new Pane() {
    };//w w w  .j  av a 2s  .co m
    mvPane.getChildren().add(mediaView);
    mvPane.setStyle("-fx-background-color: black;");
    setCenter(mvPane);
    mediaBar = new HBox();
    mediaBar.setAlignment(Pos.CENTER);
    mediaBar.setPadding(new Insets(5, 10, 5, 10));
    BorderPane.setAlignment(mediaBar, Pos.CENTER);

    final Button playButton = new Button(">");

    playButton.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            Status status = mp.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) {
                    mp.seek(mp.getStartTime());
                    atEndOfMedia = false;
                }
                mp.play();
            } else {
                mp.pause();
            }
        }
    });
    mp.currentTimeProperty().addListener(new InvalidationListener() {
        public void invalidated(Observable ov) {
            updateValues();
        }
    });

    mp.setOnPlaying(new Runnable() {
        public void run() {
            if (stopRequested) {
                mp.pause();
                stopRequested = false;
            } else {
                playButton.setText("||");
            }
        }
    });

    mp.setOnPaused(new Runnable() {
        public void run() {
            System.out.println("onPaused");
            playButton.setText(">");
        }
    });

    mp.setOnReady(new Runnable() {
        public void run() {
            duration = mp.getMedia().getDuration();
            updateValues();
        }
    });

    mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1);
    mp.setOnEndOfMedia(new Runnable() {
        public void run() {
            if (!repeat) {
                playButton.setText(">");
                stopRequested = true;
                atEndOfMedia = true;
            }
        }
    });
    mediaBar.getChildren().add(playButton);
    // Add spacer
    Label spacer = new Label("   ");
    mediaBar.getChildren().add(spacer);

    // Add Time label
    Label timeLabel = new Label("Time: ");
    mediaBar.getChildren().add(timeLabel);

    // Add time slider
    timeSlider = new Slider();
    HBox.setHgrow(timeSlider, Priority.ALWAYS);
    timeSlider.setMinWidth(50);
    timeSlider.setMaxWidth(Double.MAX_VALUE);

    timeSlider.valueProperty().addListener(new InvalidationListener() {
        public void invalidated(Observable ov) {
            if (timeSlider.isValueChanging()) {
                // multiply duration by percentage calculated by slider position
                mp.seek(duration.multiply(timeSlider.getValue() / 100.0));
            }
        }
    });

    mediaBar.getChildren().add(timeSlider);

    // Add Play label
    playTime = new Label();
    playTime.setPrefWidth(130);
    playTime.setMinWidth(50);
    mediaBar.getChildren().add(playTime);

    // Add the volume label
    Label volumeLabel = new Label("Vol: ");
    mediaBar.getChildren().add(volumeLabel);

    // Add Volume slider
    volumeSlider = new Slider();
    volumeSlider.setPrefWidth(70);
    volumeSlider.setMaxWidth(Region.USE_PREF_SIZE);
    volumeSlider.setMinWidth(30);
    volumeSlider.valueProperty().addListener(new InvalidationListener() {
        public void invalidated(Observable ov) {
            if (volumeSlider.isValueChanging()) {
                mp.setVolume(volumeSlider.getValue() / 100.0);
            }
        }
    });
    mediaBar.getChildren().add(volumeSlider);
    setBottom(mediaBar);
}