Search code examples
javajavafxmedia-playerscenebuilderseekbar

how can i fix my progress and seek slider to work for all media?


so im making a media player in javafx (eclipse) and i used to slider as a progress bar and scrubber but for some reason the sider only shows the progress for the first media , as soon as i press next the progress doesnt show anymore. i can still seek to the position i was to but progress wont show Here is the code for the main class i have a class play List item which holds the names for all the files in a given directory

package application;
import java.io.File;
import java.net.URL; 
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.util.Duration;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.event.EventHandler;

public class MainController implements Initializable {
    @FXML
    private MediaView mv;
    private MediaPlayer mp;
    private Media me;
    @FXML
    private Label title;
    @FXML
    Slider volslider;
    int pointer = 0;
    @FXML
    Slider progBar;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        String path = new File("src/media/" + application.Playlist.playnext(pointer)).getAbsolutePath();
        me = new Media(new File(path).toURI().toString());
        mp = new MediaPlayer(me);
        mv.setMediaPlayer(mp);
        // mp.setAutoPlay(true);
        DoubleProperty width = mv.fitWidthProperty();
        DoubleProperty height = mv.fitHeightProperty();
        width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
        height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));

        mp.setOnReady(new Runnable() {

            @Override
            public void run() {

                volslider.setValue(mp.getVolume() * 100);

                volslider.valueProperty().addListener(new InvalidationListener() {

                    @Override
                    public void invalidated(Observable observable) {
                        mp.setVolume(volslider.getValue() / 100);
                    }

                });
                mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {

                    @Override
                    public void changed(ObservableValue<? extends Duration> observable, Duration oldValue,
                            Duration newValue) {
                        progBar.setValue((newValue.toSeconds() / mp.getTotalDuration().toSeconds()) * 100);
                    }
                });
                progBar.setOnMouseClicked(new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        mp.seek(Duration.seconds((progBar.getValue() / 100) * mp.getTotalDuration().toSeconds()));

                    }
                });

            }
        });
    }

    public void play(ActionEvent event) {

        mp.play();

    }

    public void pause(ActionEvent event) {
        mp.pause();
    }

    public void speedup(ActionEvent event) {
        mp.setRate(2);
    }

    public void normalspeed(ActionEvent event) {
        mp.setRate(1);
    }

    public void slowmo(ActionEvent event) {
        mp.setRate(0.5);
    }

    public void reload(ActionEvent event) {
        progBar.setValue(0);
        mp.seek(mp.getStartTime());
        mp.stop();
    }

    public void end(ActionEvent event) {
        progBar.setValue(100);
        mp.seek(mp.getTotalDuration());
        mp.stop();
    }

    public void next(ActionEvent event) {
        progBar.setValue(0);
        mp.seek(mp.getTotalDuration());
        mp.stop();
        pointer++;
        pointer = application.Playlist.checkrange(pointer);
        String path = new File("src/media/" + application.Playlist.playnext(pointer)).getAbsolutePath();
        me = new Media(new File(path).toURI().toString());
        mp = new MediaPlayer(me);
        mv.setMediaPlayer(mp);
        mp.play();

    }

    public void previous(ActionEvent event) {
        // progBar.setValue(0);
        mp.seek(mp.getTotalDuration());
        mp.stop();
        pointer--;
        pointer = application.Playlist.checkrange(pointer);
        String path = new File("src/media/" + application.Playlist.playnext(pointer)).getAbsolutePath();
        me = new Media(new File(path).toURI().toString());
        mp = new MediaPlayer(me);
        mv.setMediaPlayer(mp);
        mp.play();

    }
}

Solution

  • In your next() method you create a new MediaPlayer but you don't update the reference to your currentTime property which has been bound in the initialize function. If you want to change that, unbind the old MediaPlayer's currentTime property and then bind it to your new MediaPlayer instance in next().