媒体对象上的javafx UNKNOWN持续时间 [英] javafx UNKNOWN duration on Media object

查看:193
本文介绍了媒体对象上的javafx UNKNOWN持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和JavaFX的新手,我花了几年时间用QT开发Python,现在我正在开发Java和JavaFX。

I am a newbie to both Java and JavaFX, I've spent the last couple years developing on Python with QT, and I am now developing in Java and JavaFX.

我正在开发一个程序,为用户设置时间播放音乐文件,然后停止。就这样,我需要从媒体对象获取持续时间以告诉音频何时停止(基于用户时间输入),但是,媒体对象上的getDuration()方法总是返回UNKNOWN。

I am developing a program that plays music files for the users set time and then stops. Be that as such, I am in need of getting the duration from the Media Object in order to tell the audio when to stop (based on the users time input), however, the getDuration() method on a media object always returns UNKNOWN.

我在Windows 8.1和openSuSE 13.2上都使用JDK8u25(最新的稳定版本)(两者都有相同的问题):

I am using JDK8u25 (the latest stable release) on both Windows 8.1 and openSuSE 13.2 (both have the same issue):

File filestring = new File("my/file/dir/file.mp3")
Media file = new Media(filestring.toURI().toString());
file.getDuration(); // Returns UNKNOWN on both wav and mp3 files
file.getDuration().toMinutes()) // Also tested with toHours() toSeconds(), toMilli()...
// Above returns NaN because the file's duration is unknown

正如测试一样,我把整个mp3音乐收藏(1,000 +同时包含CBR和VBR的歌曲)并且java遍历每个文件并且所有这些都表示持续时间是未知的。我也试过来自多个不同来源的wav文件(只是为了确保它不是mp3格式),看看是否改变了......没有;只有UNKNOWN持续时间。我还检查了标签是否就位,其他每个程序都说这些文件的文件属性的长度或持续时间都很好。

Just as a test, I put my whole mp3 music collection (1,000+ songs with both CBR and VBR) and had java iterate through each file and all of them said duration was UNKNOWN. I also tried wav files from multiple different sources (just to make sure it wasn't the mp3 format) to see if that changed and...nothing; nothing but UNKNOWN for duration. I also checked to make sure the tags are inplace which every other program says the length or duration in the files properties for these files are good.

文件的奇怪之处在于当我将Media对象传递给MediaPlayer时,播放效果非常好。虽然我可以使用外部库来获取长度,但是看到整个程序使用java和javafx库,这将更容易和更简化。

What's weird is that the file plays perfectly when I pass the Media object into a MediaPlayer. Though I could use an external library for getting the length, but seeing as the whole program uses java and javafx libraries, this would be much easier and more streamlined.

推荐答案

如果您阅读媒体的javadoc class,它说你应该等待媒体播放器做好准备:

If you read the javadoc for Media class, it says you should wait for the media player to be ready:


媒体信息是异步获得的,因此不一定可以立即获得该类的实例化。但是,如果实例已与MediaPlayer关联并且该播放器已转换为Status.READY状态

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 Status.READY status

那么所有信息都应该可用。所以你只需要创建一个 MediaPlayer 的实例,并监听 Status.READY

So you just need to create an instance of MediaPlayer and listen for the Status.READY:

    File filestring = new File("my/file/dir/file.mp3");
    Media file = new Media(filestring.toURI().toString());  

    MediaPlayer mediaPlayer = new MediaPlayer(file);

    mediaPlayer.setOnReady(new Runnable() {

        @Override
        public void run() {

            System.out.println("Duration: "+file.getDuration().toSeconds());

            // display media's metadata
            for (Map.Entry<String, Object> entry : file.getMetadata().entrySet()){
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }

            // play if you want
            mediaPlayer.play();
        }
    });

这篇关于媒体对象上的javafx UNKNOWN持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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