Retrieving metadata from media files in JavaFX

旧巷老猫 提交于 2021-01-28 03:11:32

问题


I've a MediaPlayer which plays the music chosen by the user from the library. While creating the library, the songs are required to be listed by their title. I know I can acquire all metadatas using a Media object. But that is arising two problems. I) The whole process is very time and resource consuming for a large media collection II) Even if I do so using the following code

for(String path : paths){
      Media m = new Media(path);
      String title = (String)m.getMetadata().get("title");
      String album = (String)m.get metadata().get("album");
      String artist = (String)m.get metadata().get("artist");
}

Most of the times the artist,title,album strings are having null value though the file itself has metadata. Analyzing Media class I understood that there's some kind of threading inside of it while acquiring metadata and that's why the rapid request is returning null. But I can't do a wait(milis) every time because that'll be massive time consuming. Any workarounds??


回答1:


From the Media documentation:

The media information is obtained asynchronously and so not necessarily available immediately after instantiation of the class. All information should however be available if the instance has been associated with a MediaPlayer and that player has transitioned to MediaPlayer.Status.READY status. To be notified when metadata or Tracks are added, observers may be registered with the collections returned by getMetadata() and getTracks(), respectively.

So, you will probably need to add listeners and run your processing inside the listeners. The listeners could be added either on the metadata directly or on the status of an associated player. For example:

Media media = new Media(myMediaLocationString);
media.getMetadata().addListener((MapChangeListener<String, Object>) change -> {
    // code to process metadata attribute change.
});
MediaPlayer player = new MediaPlayer(media);

I haven't tried testing this with a sample program, but, from my understanding of the documentation, it should work ;-)

memory usage. Creating 2000 Media objects is really freezing the display. What to do?

Freezing and memory consumption are different things and can be unrelated.

To prevent the UI thread blocking, don't block the UI thread while creating media objects, use concurrency facilities. There are many example for different scenarios in the Task documentation. Be careful when writing concurrent code that you do not modify anything in an active scene graph (e.g. being displayed to the user).

To reduce memory consumption, don't keep references to media you no longer need and appropriately dispose of media player resources in a timely fashion.

Also consider if you really need to create 2000 media objects or whether it would be preferred to create just a subset of that amount initially and (perhaps) create additional objects dynamically on an as needs based.



来源:https://stackoverflow.com/questions/34186951/retrieving-metadata-from-media-files-in-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!