: 리스너는 ArrayList<String>
그것에 아무것도 하나 개의 객체를 가진 결과 향상된 루프가 완료되면 방법을 작동 여기
은 샘플입니다 청취자를 추가하여 미디어 소스 메타 데이터를 읽는 다른 방법은 해당 정보를 mediaplayer .setOnReady(); 여기
공용 클래스 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을 사용하게 한 몇 가지 변경
코드에서 찾을 수 있습니다.
synchronized(obj){
obj.wait(100);
}
미디어 플레이어 .setOnReady()가 새 스레드를 만들고 루프가 주요 응용 프로그램 스레드, 루프가 다른 스레드가 만들어지기 전에 잠시 기다려야하고 메타 데이터를 추출 할 수 있으며 .setOnReady()에는
synchronized(obj){
obj.notify;
}
이 있으므로 주 스레드를 깨우므로 루프 다음 항목으로 이동할 수 있습니다
나는 이것이 최선의 해결책은 아니라고 인정하지만 파일 목록에서 JavaFx 미디어 메타 데이터를 읽는 방법에 대해 더 나은 방법을 가지고있는 사람이면 누구나 환영합니다. 전체 Netbeans 프로젝트는 여기에서 찾을 수 있습니다 https://docs.google.com/file/d/0BxDEmOcXqnCLSTFHbTVFcGIzT1E/edit?usp=sharing
더하기 메타 데이터 사용을 제거하는 JavaFX를 사용하여 작은 MediaPlayer 응용 프로그램을 만들었습니다 https://docs.google.com/file/d/0BxDEmOcXqnCLR1Z0VGN4ZlJkbUU/edit?usp=sharing