我在编码方面非常陌生,目前正在尝试使用JavaFX对音乐播放器进行编码。我正在使用Eclipse和Scene Builder,目前正在尝试在我的songName label
上获取文件名。但是,启动程序时,文件名未显示在标签中。
我的错误在哪里,您还看到其他错误或改进之处吗?
public class MainController implements Initializable {
@FXML
private MediaPlayer mp;
@FXML
private Slider volumeSlider;
@FXML
private String filePath;
@FXML
private Slider seekSlider;
private Label timeLabel;
private Duration duration;
private Label songName;
@FXML
private void handleButtonAction (ActionEvent event)
{
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Select a File (*.mp3)", "*.mp3");
fileChooser.getExtensionFilters().add(filter);
File file = fileChooser.showOpenDialog(null);
filePath = file.toURI().toString();
if(filePath != null)
{
Media media = new Media(filePath);
mp = new MediaPlayer(media);
mp.play();
volumeSlider.setValue(mp.getVolume() * 100);
volumeSlider.valueProperty().addListener(new InvalidationListener()
{
@Override
public void invalidated(Observable observable)
{
mp.setVolume(volumeSlider.getValue() / 200);
}
});
mp.currentTimeProperty().addListener(new ChangeListener<Duration>() {
@Override
public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue)
{
seekSlider.setValue(newValue.toSeconds());
}
});
seekSlider.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mp.seek(Duration.seconds(seekSlider.getValue()));
}
});
}
}
public void nameSong()
{
songName.setText(filePath.toString());
}
protected void updateValues() {
if (timeLabel != null && seekSlider != null && volumeSlider != null) {
Platform.runLater(new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
Duration currentTime = mp.getCurrentTime();
timeLabel.setText(formatTime(currentTime, duration));
seekSlider.setDisable(duration.isUnknown());
if (!seekSlider.isDisabled()
&& duration.greaterThan(Duration.ZERO)
&& !seekSlider.isValueChanging()) {
seekSlider.setValue(currentTime.divide(duration).toMillis()
* 100.0);
}
if (!volumeSlider.isValueChanging()) {
volumeSlider.setValue((int)Math.round(mp.getVolume()
* 100));
}
}
});
}
}
private static String formatTime(Duration elapsed, Duration duration) {
int intElapsed = (int)Math.floor(elapsed.toSeconds());
int elapsedHours = intElapsed / (60 * 60);
if (elapsedHours > 0) {
intElapsed -= elapsedHours * 60 * 60;
}
int elapsedMinutes = intElapsed / 60;
int elapsedSeconds = intElapsed - elapsedHours * 60 * 60
- elapsedMinutes * 60;
if (duration.greaterThan(Duration.ZERO)) {
int intDuration = (int)Math.floor(duration.toSeconds());
int durationHours = intDuration / (60 * 60);
if (durationHours > 0) {
intDuration -= durationHours * 60 * 60;
}
int durationMinutes = intDuration / 60;
int durationSeconds = intDuration - durationHours * 60 * 60 -
durationMinutes * 60;
if (durationHours > 0) {
return String.format("%d:%02d:%02d/%d:%02d:%02d",
elapsedHours, elapsedMinutes, elapsedSeconds,
durationHours, durationMinutes, durationSeconds);
} else {
return String.format("%02d:%02d/%02d:%02d",
elapsedMinutes, elapsedSeconds,durationMinutes,
durationSeconds);
}
} else {
if (elapsedHours > 0) {
return String.format("%d:%02d:%02d", elapsedHours,
elapsedMinutes, elapsedSeconds);
} else {
return String.format("%02d:%02d",elapsedMinutes,
elapsedSeconds);
}
}
}
@FXML
public void play(ActionEvent event)
{
mp.play();
mp.setRate(1);
}
@FXML
public void pause(ActionEvent event)
{
mp.pause();
}
@FXML
public void stop(ActionEvent event)
{
mp.stop();
}
@FXML
public void slow(ActionEvent event)
{
mp.setRate(0.5);
}
@FXML
public void fast(ActionEvent event)
{
mp.setRate(2);
}
@FXML
public void reload(ActionEvent event)
{
mp.seek(mp.getStartTime());
mp.play();
}
@FXML
public void next(ActionEvent event)
{
mp.seek(mp.getTotalDuration());
mp.play();
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
答案 0 :(得分:0)
您绝不会尝试将歌曲的名称设置为Label
。您拥有nameSong()
方法,但从未在代码中调用它。
您只需要在想要更新Label
的某个时间调用它即可。