JavaFX MediaPlayer play audio file

Description

JavaFX MediaPlayer play audio file

import java.io.File;
import java.net.URI;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.media.Media;
import javafx.scene.media.MediaErrorEvent;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class Main extends Application {
   private MediaPlayer mediaPlayer;
   private MediaView mediaView;
   private Slider progressSlider;
   private ChangeListener<Duration> progressListener;

   public static void main(String[] args) {
      Application.launch(args);//w ww  .j  av  a 2  s  . co m
   }

   @Override
   public void start(final Stage primaryStage) {
      primaryStage.setTitle("Playing Video");
      primaryStage.centerOnScreen();
      primaryStage.initStyle(StageStyle.TRANSPARENT);

      final Group root = new Group();
      final Scene scene = new Scene(root, 540, 300, Color.rgb(0, 0, 0, 0));

      progressSlider = createSlider(scene);
      root.getChildren().add(progressSlider);

      // update slider as video is progressing (later removal)
      progressListener = (ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
         progressSlider.setValue(newValue.toSeconds());
      };
      URI resourceUrlOrFile = null;

      String filePath = "your audio file";

      resourceUrlOrFile = new File(filePath).toURI();
      // load media
      Media media = new Media(resourceUrlOrFile.toString());
      // create a new media player
      mediaPlayer = new MediaPlayer(media);

      // stop previous media player and clean up
      if (mediaPlayer != null) {
         mediaPlayer.stop();
         mediaPlayer.currentTimeProperty().removeListener(progressListener);
         mediaPlayer.setOnPaused(null);
         mediaPlayer.setOnPlaying(null);
         mediaPlayer.setOnReady(null);
      }

      // as the media is playing move the slider for progress
      mediaPlayer.currentTimeProperty().addListener(progressListener);

      // play video when ready status
      mediaPlayer.setOnReady(() -> {
         progressSlider.setValue(1);
         progressSlider.setMax(mediaPlayer.getMedia().getDuration().toMillis() / 1000);
         mediaPlayer.play();
      });

      // Lazy init media viewer
      if (mediaView == null) {
         mediaView = new MediaView();
         mediaView.setMediaPlayer(mediaPlayer);
         mediaView.setX(4);
         mediaView.setY(4);
         mediaView.setPreserveRatio(true);
         mediaView.setOpacity(.85);
         mediaView.setSmooth(true);

         mediaView.fitWidthProperty().bind(scene.widthProperty().subtract(220));
         mediaView.fitHeightProperty().bind(scene.heightProperty().subtract(30));

         // make media view as the second node on the scene.
         root.getChildren().add(1, mediaView);
      }

      // sometimes loading errors occur, print error when this happens
      mediaView.setOnError((MediaErrorEvent event1) -> {
         event1.getMediaError().printStackTrace();
      });

      mediaView.setMediaPlayer(mediaPlayer);
      // stop       
      if (mediaPlayer != null) {
         mediaPlayer.stop();
      }
      //mediaPlayer.pause();
      //mediaPlayer.play();

      primaryStage.setScene(scene);
      primaryStage.show();
   }
   private Slider createSlider(Scene scene) {
      Slider slider = new Slider();
      slider.setMin(0);
      slider.setMax(100);
      slider.setValue(1);
      slider.setShowTickLabels(true);
      slider.setShowTickMarks(true);

      slider.valueProperty()
            .addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
               if (slider.isPressed()) {
                  long dur = newValue.intValue() * 1000;
                  mediaPlayer.seek(new Duration(dur));
               }
            });

      slider.translateYProperty().bind(scene.heightProperty().subtract(30));
      return slider;
   }
}



PreviousNext

Related