title: How to print duration of media and style slider | JavaFX
body: <p>I am making a music player for my college project and facing some issues with Sliders.</p>
  
  <h2>Here are the things I want to do with Seeking Bar:</h2>
  
  <ol>
  <li>How to style the seeking slider? (-fx-accent is not working).</li>
  <li>How to print total time of song, and the current time at which song is playing.</li>
  </ol>
  
  <p>Below is the code I'm using for seekSlider: </p>
  
  <pre><code>package musicplayer;
  
  import java.io.File;
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.beans.Observable;
  import javafx.beans.binding.Bindings;
  import javafx.beans.value.ObservableValue;
  import javafx.collections.MapChangeListener;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.control.Slider;
  import javafx.scene.image.Image;
  import javafx.scene.image.ImageView;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.media.Media;
  import javafx.scene.media.MediaPlayer;
  import javafx.scene.media.MediaView;
  import javafx.stage.FileChooser;
  import javafx.util.Duration;
  
  
  
  public class FXMLDocumentController implements Initializable {
  
      @FXML
      private Button playbtn;
      @FXML
      private Button pausebtn;
      @FXML
      private Button stopbtn;
      @FXML
      private Button open;
      @FXML
      private Slider seekSlider;
      @FXML
      private Slider volumeSlider;
      @FXML
      private Label durationLabel;
      @FXML
      private Label musicBox;
      @FXML
      private Label playTime;
      @FXML
      private ImageView albumCover;
      @FXML
      private ImageView playbtnimg;
  
      @FXML
      private MediaView mv; 
      private MediaPlayer mp;
      private Media me;
      Double duration;
      File selectedFile = null;
      /**
       * Initializes the controller class.
       * @param e
       */
  
      //Open button code for choosing file through FileChooser
       public void open(ActionEvent e){
  
  
          FileChooser fc = new FileChooser();
          //Extension filter for showing mp3 and m4a files
          fc.getExtensionFilters().addAll(new
          FileChooser.ExtensionFilter("MP3", "*.mp3"),new
          FileChooser.ExtensionFilter("M4A", "*.m4a"));
          selectedFile = fc.showOpenDialog(null);
  
          //If runs when user selects a file
          if(selectedFile != null){
  
          String path = selectedFile.getAbsolutePath(); //gets the path of file
          if(mp!=null) //This if works when any song is playing
          {
              mp.stop(); //Stops the currently playing music
  
              //Change the album art to default.png
              File file = new File("src/musicplayer/default.png");
              Image image = new Image(file.toURI().toString());
              albumCover.setImage(image);
          }
  
          path = path.replace("\\","/");
          me = new Media(new File(path).toURI().toString());
          mp = new MediaPlayer(me);
          mv.setMediaPlayer(mp);
          //Code for Volume Slider 
          volumeSlider.setValue(0.7 * 100);
          volumeSlider.valueProperty().addListener((Observable observable) -&gt; {
          mp.setVolume(volumeSlider.getValue()/100);
          });
          //Code for seeking bar
  
          seekSlider.maxProperty().bind(Bindings.createDoubleBinding(
              () -&gt; mp.getTotalDuration().toSeconds(),
              mp.totalDurationProperty()));
  
          mp.currentTimeProperty().addListener((ObservableValue&lt;? extends Duration&gt; observable, Duration oldValue, Duration newValue) -&gt; {
              seekSlider.setValue(newValue.toSeconds());
          });
  
          seekSlider.setOnMouseClicked((MouseEvent event) -&gt; {
              mp.seek(Duration.seconds(seekSlider.getValue()));
          });
  
  
  
  
  
  
          mp.play(); //Plays the selected song
  
          me.getMetadata().addListener(new MapChangeListener&lt;String, Object&gt;() {
          @Override
          public void onChanged(Change&lt;? extends String, ? extends Object&gt; ch) {
            if (ch.wasAdded()) {
              handleMetadata(ch.getKey(), ch.getValueAdded());
            }
          }
  
              private void handleMetadata(String key, Object value) {        
                  if(key.equals("title")){   
                      musicBox.setText(value.toString());
                      }
                  if(key.equals("image")){
                      albumCover.setImage((Image)value);
                      }
                  }
              });
          }
      }
      public void play(ActionEvent e){
      mp.play();
      }
  
      public void pause(ActionEvent e){
      mp.pause();
      }
  
      public void stop(ActionEvent e){
      mp.stop();
      }
  
  
      @Override
      public void initialize(URL url, ResourceBundle rb) {
  
  
      }
  
  
  
  }
  </code></pre>
  
  <p>I am using playTime Label to display totalDuration and durationLabel to display current time.
  Also, check the code of seekSlider, Sometimes, it does not works properly while dragging. </p>