从JavaFX中的媒体文件中检索元数据 [英] Retrieving metadata from media files in JavaFX

查看:74
本文介绍了从JavaFX中的媒体文件中检索元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MediaPlayer,它可以播放用户从库中选择的音乐.创建库时,要求按歌曲名称列出歌曲.我知道我可以使用Media对象获取所有元数据.但这引起了两个问题. I)整个过程非常耗时且耗费大量媒体资源 II)即使我使用下面的代码

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");
}

尽管文件本身具有元数据,但在大多数情况下,艺术家,标题,专辑字符串具有null值.通过分析Media类,我了解到在获取元数据时,它内部存在某种线程,这就是为什么快速请求返回null的原因.但是我不能每次都等待(milis),因为那会浪费大量时间.任何解决方法?

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??

推荐答案

来自

媒体信息是异步获取的,因此不一定在类实例化后立即可用.但是,如果实例已与MediaPlayer关联并且该播放器已转换为MediaPlayer.Status.READY状态,则所有信息都应该可用.为了在添加元数据或跟踪时得到通知,可以向因此,您可能需要添加侦听器并在侦听器内部运行处理.侦听器可以直接添加到元数据上,也可以添加到关联播放器的状态上.例如:

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 ;-)

内存使用情况.创建2000 Media对象确实冻结了显示.该怎么办?

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.

要防止UI线程阻塞,请在创建媒体对象时不要阻塞UI线程,请使用任务文档.编写并发代码时要小心,不要在活动场景图中修改任何内容(例如,向用户显示).

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.

还要考虑是否确实需要创建2000个媒体对象,或者是否更希望最初仅创建该数量的子集,并且(也许)根据需要动态创建其他对象.

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.

这篇关于从JavaFX中的媒体文件中检索元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆