Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>the other way to read a media source metadata with adding a listener is extract that information in the mediaplayer .setOnReady(); here is an example part of the java controller class</p> <p>public class uiController implements Initializable {</p> <pre><code>@FXML private Label label; @FXML private ListView&lt;String&gt; lv; @FXML private AnchorPane root; @FXML private Button button; private ObservableList&lt;String&gt; ol= FXCollections.observableArrayList(); private List&lt;File&gt; 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 &amp;&amp;!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) } } }); } </code></pre> <p>}</p> <p>the few changes that have made is used an ObservableList to store the artist name from the metadata</p> <p>in the code you will find this <pre> synchronized(obj){ obj.wait(100); } </pre> I do this because the mediaplayer .setOnReady() creates a new thread and the loop is in the main application thread, The loop has to wait for some time before the other thread is created and we are able to extract the metadata, and in the .setOnReady() there is a <pre> synchronized(obj){ obj.notify; } </pre> to wake up the main thread hence the loop is able to move to the next item</p> <p>I admit that this may not be the best solution to do this but am welcomed to anyone who has any better way on how to read JavaFx media metadata from a list of files The full Netbeans project can be found here <a href="https://docs.google.com/file/d/0BxDEmOcXqnCLSTFHbTVFcGIzT1E/edit?usp=sharing" rel="nofollow">https://docs.google.com/file/d/0BxDEmOcXqnCLSTFHbTVFcGIzT1E/edit?usp=sharing</a></p> <p>plus have created a small MediaPlayer Application using JavaFX which expolits use of the metadata <a href="https://docs.google.com/file/d/0BxDEmOcXqnCLR1Z0VGN4ZlJkbUU/edit?usp=sharing" rel="nofollow">https://docs.google.com/file/d/0BxDEmOcXqnCLR1Z0VGN4ZlJkbUU/edit?usp=sharing</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload