本文整理匯總了Java中javafx.scene.media.MediaPlayer.setOnReady方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaPlayer.setOnReady方法的具體用法?Java MediaPlayer.setOnReady怎麽用?Java MediaPlayer.setOnReady使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javafx.scene.media.MediaPlayer
的用法示例。
在下文中一共展示了MediaPlayer.setOnReady方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: loadingParam
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
private void loadingParam() {
File paramFile = new File("res/Parameters");
Scanner sc;
try {
sc = new Scanner(paramFile);
if (sc.hasNextLine()) {
isRandom = Boolean.valueOf(sc.nextLine());
musicIndex = Integer.valueOf(sc.nextLine());
}
hit = new Media(musicList.get(musicIndex).toURI().toString());
mediaPlayer = new MediaPlayer(hit);
mediaPlayer.setOnReady(this::playMusic);
} catch (IOException e) {
e.printStackTrace();
}
}
示例2: loadVideo
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public MediaPlayer loadVideo(int videoIndex) {
LoggerFacade.INSTANCE.debug(this.getClass(), "Load video"); // NOI18N
final StringBuilder sb = new StringBuilder();
sb.append(PREFIX__VIDEO);
sb.append(videoIndex);
sb.append(SUFFIX__FILE);
final String video = PropertiesFacade.INSTANCE.getProperty(KEY__RESOURCE_BUNDLE__VIDEO, sb.toString());
try {
final Media media = new Media(this.getClass().getResource(video).toURI().toString());
final MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
mediaPlayer.setOnReady(() -> {
mediaPlayer.play();
});
return mediaPlayer;
} catch (URISyntaxException ex) {
LoggerFacade.INSTANCE.error(this.getClass(), "Can't load video: " + video, ex); // NOI18N
}
return null;
}
示例3: from
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public static Video from(final URI path) {
Media media = new Media(path.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
Video video = new Video(Paths.get(path).getFileName().toString(), path);
mediaPlayer.setOnReady(() -> video.duration.set(Playable.format(media.getDuration())));
return video;
}
示例4: assureMediaPlayer
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
private boolean assureMediaPlayer(FileItem item, Runnable action) {
try {
int index = musicListView.getItems().indexOf(item);
if (index > -1 && !item.isPhantomProperty().get()) {
if (!players.containsKey(item)) {
final MediaPlayer player = new MediaPlayer(new Media(toFileURI(item.getFile())));
System.out.println(player + " initialized");
player.setOnReady(action);
player.setOnEndOfMedia(()->{
playNext();
});
player.totalDurationProperty()
.addListener((obs, oldDuration, newDuration) -> playSlider.setMax(newDuration.toSeconds()));
player.currentTimeProperty().addListener((obs, oldTime, newTime) -> {
if (!playSlider.isValueChanging()) {
playSlider.setValue(player.currentTimeProperty().get().toSeconds());
}
});
players.put(item, player);
} else {
action.run();
}
return true;
}
} catch (Exception e) {
Logger.error(e);
}
return false;
}
示例5: getMediaPlayerFor
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
private MediaPlayer getMediaPlayerFor(Media media) {
try {
playerReady = false;
final MediaPlayer mediaPlayer = new MediaPlayer(media);
if(mediaPlayer.getError() == null) {
mediaPlayer.setAutoPlay(false);
mediaPlayer.currentTimeProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
Duration newDuration = (Duration) newValue;
progressBar.setValue((int) Math.round(newDuration.toSeconds()));
updateTimeLabel();
}
});
mediaPlayer.setOnReady(new Runnable() {
public void run() {
playerReady = true;
progressBar.setMinimum(0.0);
progressBar.setValue(0.0);
progressBar.setMaximum(mediaPlayer.getTotalDuration().toSeconds());
refreshLayout();
}
});
mediaPlayer.setOnError(new Runnable() {
public void run() {
processError(mediaPlayer.getError());
}
});
return mediaPlayer;
}
else {
processError(mediaPlayer.getError());
}
}
catch(Exception e) {
processError(e);
}
return null;
}
示例6: openMedia
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public void openMedia(String aMediaUri) {
if (null == aMediaUri) return;
closeMedia();
//
Media media = new Media(aMediaUri);
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView();
mediaView.setMediaPlayer(mediaPlayer);
//
mediaView.setPreserveRatio(true);
mediaView.setSmooth(true);
//
mediaView.fitHeightProperty().bind(heightProperty());
mediaView.fitWidthProperty().bind(widthProperty());
//
mediaPlayer.volumeProperty().bind(VOLUME);
mediaPlayer.setOnReady(() -> {
final Media mediaReady = mediaPlayer.getMedia();
if (null != mediaReady) {
updateBoundsForce();
System.out.println(
String.format("Open media: %s (%dx%d)",
aMediaUri,
mediaReady.getWidth(),
mediaReady.getHeight()
));
}
});
mediaView.setOnMouseClicked((event) -> {
final MouseButton mouseButton = event.getButton();
if (MouseButton.SECONDARY == mouseButton) {
if (Status.PLAYING == mediaPlayer.getStatus()) {
mediaPlayer.pause();
}
else {
mediaPlayer.play();
}
}
else if (MouseButton.PRIMARY == mouseButton) {
Duration duration = media.getDuration();
Duration newPosition = duration.divide(getWidth()).multiply(event.getX());
mediaPlayer.seek(newPosition);
if (Status.PLAYING != mediaPlayer.getStatus()) {
mediaPlayer.play();
}
}
});
mediaView.setOnScroll((event) -> setVolume(getVolume() + event.getDeltaY() * getVolumeZoom()));
//
MEDIA_VIEW.set(mediaView);
getChildren().add(mediaView);
updateBoundsForce();
}
示例7: start
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
@Override
public void start(Stage primaryStage)
{
File fileForMusic = new File("C:\\Users\\Eric\\Music\\New stuff\\Prism.mp3");
if (!fileForMusic.exists())
{
System.out.println("File does not exist");
return;
}
System.out.println(fileForMusic.toURI().toString());
// Create the m
Media song = new Media(fileForMusic.toURI().toString());
final MediaPlayer player = new MediaPlayer(song);
player.setAutoPlay(true);
player.setOnError(new Runnable() {
@Override
public void run() {
System.out.println("ERROR");
MediaException e = player.getError();
e.printStackTrace();
}
});
System.out.println(player.getStatus());
System.out.println(player);
System.out.println(player.getMedia());
player.setOnReady(new Runnable() {
@Override
public void run() {
System.out.println("Player running?");
player.play();
}
});
// Add a mediaView, to display the media. Its necessary !
// This mediaView is added to a Pane
MediaView mediaView = new MediaView(player);
mediaView.setMediaPlayer(player);
// Setup the Java FX Scene
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
root.getChildren().add(mediaView);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
示例8: createScene
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public Scene createScene(final String sourceURL) throws Throwable {
final Group root = new Group();
root.autosize();
MediaUtils.activeMedia = sourceURL;
final String trailer = MediaUtils.getMediaURL(sourceURL);
media = new Media(trailer);
player = new MediaPlayer(media);
view = new MediaView(player);
view.setFitWidth(1);
view.setFitHeight(1);
view.setPreserveRatio(false);
// System.out.println("media.width: "+media.getWidth());
final Scene scene = new Scene(root, 1, 1, Color.BLACK);
player.play();
player.setOnReady(() -> {
ControlPanel.seek().setValue(0);
});
player.currentTimeProperty().addListener(
(observableValue, duration, current) -> {
final long currentTime = (long) current.toMillis();
final long totalDuration = (long) player.getMedia()
.getDuration().toMillis();
updateTime(currentTime, totalDuration);
});
// PlayerUtils.activeYoutubeVideo = youtubeVideo;
if (sourceURL.equals("https://www.youtube.com/watch?v=kGubD7KG9FQ")) {
player.pause();
}
UISession.setMediaPlayer(player);
UISession.setMediaView(view);
UISession.setMedia(media);
return (scene);
}
示例9: setMediaLocation
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public void setMediaLocation(String mediaLocation, Consumer<JFXMovieJFrameController> onReadyRunnable) {
Preconditions.checkNotNull(mediaLocation, "The medialocation can not be null");
this.mediaLocation = mediaLocation;
Media media = new Media(mediaLocation);
final ObservableMap<String,Object> metadata = media.getMetadata();
//timecode.setFrameRate((Double) metadata.getOrDefault("framerate", DEFAULT_FRAME_RATE));
frameRate = DEFAULT_FRAME_RATE;
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
// --- Configure MediaPlayer
mediaPlayer.currentTimeProperty().addListener(observable -> updateValues());
mediaPlayer.setOnPlaying(() -> { playButton.setText("||"); });
mediaPlayer.setOnPaused(() -> { playButton.setText(">"); });
mediaPlayer.setOnReady(() -> {
Media m = mediaPlayer.getMedia();
duration = m.getDuration();
Timecode tc = new Timecode(duration.toSeconds() * frameRate, frameRate);
SwingUtilities.invokeLater(() -> maxTimecodeTextField.setText(tc.toString()));
updateValues();
onReadyRunnable.accept(this);
readyProperty.setValue(true);
});
mediaPlayer.setOnEndOfMedia(() -> { playButton.setText(">"); });
// --- Configure play button
playButton.setOnAction((e) -> {
MediaPlayer.Status status = mediaPlayer.getStatus();
System.out.println(status);
if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) {
// Do nothing
return;
}
if (status == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
}
else if (status == MediaPlayer.Status.PAUSED
|| status == MediaPlayer.Status.READY
|| status == MediaPlayer.Status.STOPPED) {
mediaPlayer.play();
}
});
// --- Configure Scrubber
scrubber.valueProperty().addListener(observable -> {
if (scrubber.isValueChanging()) {
// multiply duration by percentage calculated by slider position
mediaPlayer.seek(duration.multiply(scrubber.getValue() / 100D));
}
});
}
示例10: setMediaLocation
import javafx.scene.media.MediaPlayer; //導入方法依賴的package包/類
public void setMediaLocation(String mediaLocation) {
dispose();
Media media = new Media(mediaLocation);
final ObservableMap<String,Object> metadata = media.getMetadata();
//timecode.setFrameRate((Double) metadata.getOrDefault("framerate", DEFAULT_FRAME_RATE));
frameRate = DEFAULT_FRAME_RATE;
mediaPlayer = new MediaPlayer(media);
mediaView.setMediaPlayer(mediaPlayer);
// --- Configure play button
playButton.setOnAction((e) -> {
MediaPlayer.Status status = mediaPlayer.getStatus();
System.out.println(status);
if (status == MediaPlayer.Status.UNKNOWN || status == MediaPlayer.Status.HALTED) {
// Do nothing
return;
}
if (status == MediaPlayer.Status.PLAYING) {
mediaPlayer.pause();
}
else if (status == MediaPlayer.Status.PAUSED || status == MediaPlayer.Status.READY ||
status == MediaPlayer.Status.STOPPED) {
mediaPlayer.play();
}
});
// --- Configure MediaPlayer
mediaPlayer.currentTimeProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateValues();
}
});
mediaPlayer.setOnPlaying(() -> { playButton.setText("||"); });
mediaPlayer.setOnPaused(() -> { playButton.setText(">"); });
mediaPlayer.setOnReady(() -> {
Media m = mediaPlayer.getMedia();
duration = m.getDuration();
Timecode tc = new Timecode(duration.toSeconds() * frameRate, frameRate);
maxTimecodeTextField.setText(tc.toString());
updateValues();
SwingUtilities.invokeLater(() -> frame.setSize(m.getWidth(), m.getHeight()));
readyProperty.setValue(true);
});
mediaPlayer.setOnEndOfMedia(() -> { playButton.setText(">"); });
// --- Configure Scrubber
scrubber.valueProperty().addListener(observable -> {
if (scrubber.isValueChanging()) {
// multiply duration by percentage calculated by slider position
mediaPlayer.seek(duration.multiply(scrubber.getValue() / 100D));
}
});
}