Java Code Examples for javafx.scene.media.MediaPlayer#play()

The following examples show how to use javafx.scene.media.MediaPlayer#play() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlWebView.java    From Learn-Java-12-Programming with MIT License 9 votes vote down vote up
public void start11(Stage primaryStage) {

        Text txt1 = new Text("What a beautiful music!");
        Text txt2 = new Text("If you don't hear music, turn up the volume.");

        File f = new File("src/main/resources/jb.mp3");
        Media m = new Media(f.toURI().toString());
        MediaPlayer mp = new MediaPlayer(m);
        MediaView mv = new MediaView(mp);

        VBox vb = new VBox(txt1, txt2, mv);
        vb.setSpacing(20);
        vb.setAlignment(Pos.CENTER);
        vb.setPadding(new Insets(10, 10, 10, 10));

        Scene scene = new Scene(vb, 350, 100);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX with embedded media player");
        primaryStage.onCloseRequestProperty()
                .setValue(e -> System.out.println("Bye! See you later!"));
        primaryStage.show();

        mp.play();
    }
 
Example 2
Source File: AudioDemo.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void start(final Stage stage)
{
    stage.setTitle("Audio Demo");
    stage.show();

    // Windows and Mac OS X support WAV and MP3
    // Linux: WAV hangs, MP3 results in MediaException for unsupported format
    final File file = new File("../model/src/main/resources/examples/timer/timer.mp3");
    final Media audio = new Media(file.toURI().toString());
    player = new MediaPlayer(audio);
    player.setOnError(() -> System.out.println("Error!"));
    player.setOnStopped(() ->
    {
        System.out.println("Stopped.");
        player.dispose();
        stage.close();
    });
    player.setOnEndOfMedia( () ->
    {
        System.out.println("Done.");
        player.stop();
    });
    // Wasn't necessary with JDK9, but is with 11 on Mac OS X
    player.setStartTime(Duration.seconds(0));
    player.play();
    System.out.println("Playing...");
}
 
Example 3
Source File: HtmlWebView.java    From Learn-Java-12-Programming with MIT License 5 votes vote down vote up
public void start12(Stage primaryStage) {

        Text txt = new Text("What a beautiful movie!");

        File f = new File("src/main/resources/sea.mp4");
        Media m = new Media(f.toURI().toString());
        MediaPlayer mp = new MediaPlayer(m);
        MediaView mv = new MediaView(mp);
        //mv.autosize();
        //mv.preserveRatioProperty();
        //mv.setFitHeight();
        //mv.setFitWidth();
        //mv.fitWidthProperty();
        //mv.fitHeightProperty()

        VBox vb = new VBox(txt, mv);
        vb.setSpacing(20);
        vb.setAlignment(Pos.CENTER);
        vb.setPadding(new Insets(10, 10, 10, 10));

        Scene scene = new Scene(vb, 650, 400);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX with embedded media player");
        primaryStage.onCloseRequestProperty()
                .setValue(e -> System.out.println("Bye! See you later!"));
        primaryStage.show();

        mp.play();
    }
 
Example 4
Source File: HtmlWebView.java    From Learn-Java-12-Programming with MIT License 5 votes vote down vote up
public void start13(Stage primaryStage) {

        Text txt1 = new Text("What a beautiful movie and sound!");
        Text txt2 = new Text("If you don't hear music, turn up the volume.");

        File fs = new File("src/main/resources/jb.mp3");
        Media ms = new Media(fs.toURI().toString());
        MediaPlayer mps = new MediaPlayer(ms);
        MediaView mvs = new MediaView(mps);

        File fv = new File("src/main/resources/sea.mp4");
        Media mv = new Media(fv.toURI().toString());
        MediaPlayer mpv = new MediaPlayer(mv);
        MediaView mvv = new MediaView(mpv);

        VBox vb = new VBox(txt1, txt2, mvs, mvv);
        vb.setSpacing(20);
        vb.setAlignment(Pos.CENTER);
        vb.setPadding(new Insets(10, 10, 10, 10));

        Scene scene = new Scene(vb, 650, 500);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX with embedded media player");
        primaryStage.onCloseRequestProperty()
                .setValue(e -> System.out.println("Bye! See you later!"));
        primaryStage.show();

        mpv.play();
        mps.play();
    }
 
Example 5
Source File: JFXRepresentation.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
AudioFuture(final MediaPlayer player)
{
    this.player = player;
    // Player by default just stays in "PLAYING" state
    player.setOnEndOfMedia(() -> player.stop());
    player.play();
    logger.log(Level.INFO, "Playing " + this);
}
 
Example 6
Source File: Exercise_16_26.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
	// Create an image
	ImageView image = new ImageView(new Image(
		"http://cs.armstrong.edu/liang/common/image/flag6.gif"));

	// Create a media player
	MediaPlayer audio = new MediaPlayer(new Media(
		"http://cs.armstrong.edu/liang/common/audio/anthem/anthem6.mp3"));
	audio.play();

	// Create a line
	Line line = new Line(250, 600, 250, -70);

	// Create a pane
	Pane pane = new Pane(image);

	// Create a path transition
	PathTransition pt = new PathTransition();
	pt.setDuration(Duration.millis(70000));
	pt.setPath(line);
	pt.setNode(image);
	pt.setCycleCount(Timeline.INDEFINITE);
	pt.play();

	// Create a scene and place it in the stage
	Scene scene = new Scene(pane, 500, 500);
	primaryStage.setTitle("Exercise_16_26"); // Set the stage title
	primaryStage.setScene(scene); // Place the scene in the stage
	primaryStage.show(); // Display the stage
}
 
Example 7
Source File: MediaPlayerScene.java    From aurous-app with GNU General Public License v2.0 4 votes vote down vote up
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);
	}