リスナーを追加してメディア ソース メタデータを読み取るもう 1 つの方法は、mediaplayer .setOnReady(); でその情報を抽出することです。これは、Java コントローラー クラスの一部の例です。
public class uiController は 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)
}
}
});
}
}
行われたいくつかの変更は、ObservableList を使用して、メタデータからアーティスト名を保存します
コードでこれを見つけるでしょう
同期(オブジェクト){
obj.wait(100);
}
これを行うのは、mediaplayer .setOnReady() が新しいスレッドを作成し、ループがメイン アプリケーション スレッドにあるためです。ループは、他のスレッドが作成されるまでしばらく待機する必要があり、メタデータを抽出できるようになります。 .setOnReady() があります
同期(オブジェクト){
obj.notify;
}
メインスレッドをウェイクアップするため、ループは次のアイテムに移動できます
これが最善の解決策ではないことは認めますが、ファイルのリストから JavaFx メディア メタデータを読み取るためのより良い方法を知っている人なら誰でも歓迎します。完全な Netbeans プロジェクトはこちらhttps://docs.google .com/file/d/0BxDEmOcXqnCLSTFHbTVFcGIzT1E/edit?usp=共有
さらに、メタデータの使用を悪用する JavaFX を使用して小さな MediaPlayer アプリケーションを作成しましたhttps://docs.google.com/file/d/0BxDEmOcXqnCLR1Z0VGN4ZlJkbUU/edit?usp=sharing