la l'altro modo per leggere i metadati di una fonte multimediale con l'aggiunta di un listener è estrarre le informazioni nel mediaplayer .setOnReady(); qui è una parte ad esempio della classe controller java
public class uiController implementa Initializable {
@FXML private Label label;
@FXML private ListView<String> lv;
@FXML private AnchorPane root;
@FXML private Button button;
private ObservableList<String> ol= FXCollections.observableArrayList();
private List<File> selectedFiles;
private final Object obj= new Object();
@Override
public void initialize(URL url, ResourceBundle rb) {
assert button != null : "fx:id=\"button\" was not injected: check your FXML file 'ui.fxml'.";
assert label != null : "fx:id=\"label\" was not injected: check your FXML file 'ui.fxml'.";
assert lv != null : "fx:id=\"lv\" was not injected: check your FXML file 'ui.fxml'.";
assert root != null : "fx:id=\"root\" was not injected: check your FXML file 'ui.fxml'.";
// initialize your logic here: all @FXML variables will have been injected
lv.setItems(ol);
}
@FXML private void open(ActionEvent event) {
FileChooser.ExtensionFilter extention= new FileChooser.ExtensionFilter("Music Files", "*.mp3","*.m4a","*.aif","*.wav","*.m3u","*.m3u8");
FileChooser fc= new FileChooser();
fc.setInitialDirectory(new File(System.getenv("userprofile")));
fc.setTitle("Select File(s)");
fc.getExtensionFilters().add(extention);
selectedFiles =fc.showOpenMultipleDialog(root.getScene().getWindow());
if(selectedFiles != null &&!selectedFiles.isEmpty()){
listFiles();
}
}
/**
* Convert each fie selected to its URI
*/
private void listFiles(){
try {
for (File file : selectedFiles) {
readMetaData(file.toURI().toString());
synchronized(obj){
obj.wait(100);
}
}
} catch (InterruptedException ex) {
}
System.gc();
}
/**
* Read a Media source metadata
* Note: Sometimes the was unable to extract the metadata especially when
* i have selected large number of files reasons i don't known why
* @param mediaURI Media file URI
*/
private void readMetaData(String mediaURI){
final MediaPlayer mp= new MediaPlayer(new Media(mediaURI));
mp.setOnReady(new Runnable() {
@Override
public void run() {
String artistName=(String) mp.getMedia().getMetadata().get("artist");
ol.add(artistName);
synchronized(obj){//this is required since mp.setOnReady creates a new thread and our loopp in the main thread
obj.notify();// the loop has to wait unitl we are able to get the media metadata thats why use .wait() and .notify() to synce the two threads(main thread and MediaPlayer thread)
}
}
});
}
}
le poche modifiche che hanno fatto viene utilizzato un ObservableList per memorizzare il nome dell'artista dai metadati
nel codice si trova questo
synchronized(obj){
obj.wait(100);
}
lo faccio perché il mediaplayer .setOnReady() crea un nuovo thread e il ciclo è in il thread dell'applicazione principale, Il ciclo deve attendere un po 'di tempo prima che venga creato l'altro thread e siamo in grado di estrarre i metadati, e nel .setOnReady() c'è un
synchronized(obj){
obj.notify;
}
per riattivare il thread principale e quindi il ciclo è in grado di passare alla voce successiva
Ammetto che questa potrebbe non essere la soluzione migliore per farlo, ma sono gradita a chiunque abbia un modo migliore di leggere i metadati dei media JavaFx da un elenco di file Il progetto Netbeans può essere trovato qui https://docs.google.com/file/d/0BxDEmOcXqnCLSTFHbTVFcGIzT1E/edit?usp=sharing
plus hanno creato una piccola applicazione MediaPlayer utilizzando JavaFX che espelle l'uso dei metadati https://docs.google.com/file/d/0BxDEmOcXqnCLR1Z0VGN4ZlJkbUU/edit?usp=sharing