我怎样才能修复我的进度并寻找适用于所有媒体的滑块?
how can i fix my progress and seek slider to work for all media?
所以我在 javafx (eclipse) 中制作了一个媒体播放器,我曾经将滑块用作进度条和进度条,但出于某种原因,sider 只显示第一个媒体的进度,只要我按下一个进度不再显示了。
我仍然可以寻求我曾经的位置,但进展不会显示
这是主要代码 class
我有一个 class 播放列表项,它包含给定目录中所有文件的名称
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();
}
}
在您的 next() 方法中,您创建了一个新的 MediaPlayer,但您没有更新对已在初始化函数中绑定的 currentTime 属性 的引用。如果您想更改它,请取消绑定旧 MediaPlayer 的 currentTime 属性,然后将其绑定到 next() 中的新 MediaPlayer 实例。
所以我在 javafx (eclipse) 中制作了一个媒体播放器,我曾经将滑块用作进度条和进度条,但出于某种原因,sider 只显示第一个媒体的进度,只要我按下一个进度不再显示了。 我仍然可以寻求我曾经的位置,但进展不会显示 这是主要代码 class 我有一个 class 播放列表项,它包含给定目录中所有文件的名称
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();
}
}
在您的 next() 方法中,您创建了一个新的 MediaPlayer,但您没有更新对已在初始化函数中绑定的 currentTime 属性 的引用。如果您想更改它,请取消绑定旧 MediaPlayer 的 currentTime 属性,然后将其绑定到 next() 中的新 MediaPlayer 实例。