Example usage for javafx.scene.media Media Media

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

Introduction

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

Prototype

public Media(@NamedArg("source") String source) 

Source Link

Document

Constructs a Media instance.

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();/* w  w w  . j a v  a2s . co 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 ww  . j a  v  a2  s.  c  om

    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/*from  ww w .j ava2  s .  c om*/
        public void run() {
            System.out.println("Media error occurred: " + player.getError());
        }
    });

    return player;

}

From source file:com.helegris.szorengeteg.ui.MediaLoader.java

/**
 * Creates playable JavaFX audio from a byte array. To achieve this, it
 * creates and deletes a temporary file.
 *
 * @param audioBytes// w  w  w  .ja v  a2s  .  c o  m
 * @return audio
 * @throws IOException
 */
public Media loadAudio(byte[] audioBytes) throws IOException {
    File tempFile = File.createTempFile("audio", null);
    tempFile.deleteOnExit();
    IOUtils.write(audioBytes, new FileOutputStream(tempFile));
    Media media = new Media(tempFile.toURI().toString());
    return media;
}

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;//from w w w . jav a  2 s.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);/* w ww .j a  v  a 2 s.  c  om*/

    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: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/*ww w  .  ja v  a 2 s  .  c  om*/
        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: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  ava  2 s. c  o  m

    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: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();/*from  w  w  w .  ja  v a2s . co m*/
    };

    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:com.spankingrpgs.util.LoadStateUtils.java

private static void loadBattleMusic(String gameRoot) {
    if (!loadMusic) {
        return;//from  w w w. j a v a  2 s. co  m
    }
    Media battleMusic = new Media(
            new File(Paths.get(gameRoot, "data/music/bloodlust.mp3").toString()).toURI().toString());
    Media bossMusic = new Media(
            new File(Paths.get(gameRoot, "data/music/megaboss.mp3").toString()).toURI().toString());
    MusicMap.INSTANCE.put("bloodlust", battleMusic);
    MusicMap.INSTANCE.put("megaboss", bossMusic);
    StateModificationCommands.battleMusic = "bloodlust";
    StateModificationCommands.bossMusic = "megaboss";
}