更改字符串的值

时间:2014-04-01 21:07:06

标签: java string metadata media

我有一个简单的媒体播放器,我需要在其中一个类中检索媒体的元数据。到目前为止,我已经成功提取了元数据,但是当我尝试使用这些值时,返回的字符串是空白的。

String album = "", artist = "", title = "", year = "";
...
public void addListItems(final Pane layout, final Stage stage, final Scene scene) {

if (list != null) {
    for (final String s : list) {
    final String[] sList = s.split("\\\\");
    Media media;
    try {
        media = new Media(new File(s).toURI().toURL().toString());
        media.getMetadata().addListener(new MapChangeListener<String, Object>() {
        public void onChanged(Change<? extends String, ? extends Object> arg0) {
            handleMetadata(arg0.getKey(), arg0.getValueAdded());
        }
        });
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    lab = new Button(album + sList[sList.length-1]);
    layout.getChildren().add(lab);
    lab.setPrefWidth(scene.getWidth());
    lab.setAlignment(Pos.CENTER_LEFT);
    lab.setStyle("-fx-border-color: black");
    lab.setStyle("-fx-backgound-color: lightgray");
    lab.setTooltip(new Tooltip(lab.getText()));
    lab.setOnMouseClicked(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent arg0) {
        try {
            AudioBitsMediaPlayer.mediaPlayer = AudioBitsMediaPlayer.buildMediaPlayer(s, AudioBitsMediaPlayer.status, AudioBitsMediaPlayer.timeStatus);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        AudioBitsMediaPlayer.currentDir = new File(new File(s).getParentFile().getAbsolutePath());
        AudioBitsMediaPlayer.status.setText("Audio File");
        stage.close();
        }
    });
    scene.heightProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> arg0, Number arg1, Number arg2) {
        lab.setPrefWidth(scene.getWidth());
        layout.setPrefHeight(scene.getHeight());
        }
    });
    }
}       
}
...
public void handleMetadata(String key, Object value) {
    if (key.equals("album")) {
        album = value.toString(); //Here album returns as value.toString() correctly
    } else if (key.equals("artist")) {
        artist = value.toString();
    } else if (key.equals("title")) {
        title = value.toString();
    } else if (key.equals("year")) {
        year = value.toString();
    }
    }

我已经对这个主题进行了研究,但我看到的结果对我的情况没有帮助。我非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

请尝试这样做:

artist = (String) value; 
title  = (String) value;
year   = (String) value;

答案 1 :(得分:0)

  • 您应该使用String.valueOf(value),因为那样会 即使值为Integer或其他任何值,也能正常工作。
  • 对于value.toString(),您需要检查值是否为null,否则它将抛出NullPointerException
  • 当您尝试投射(String) value;
  • 时,也会发生同样的情况