强制JavaFx“duration”属性为已知



当我试图通过getDuration()方法获取Media对象持续时间时,它返回未知。我发现了这个论坛https://forums.oracle.com/thread/2287900这意味着它可能不会立即为人所知。

然而,那个论坛说这是一个网络问题,但我没有使用网络。那么,duration可能未知的其他原因是什么呢?我该如何找到它?

这对我有效:

Media mediaPlaying = new Media(sourcePlaying);
MediaPlayer mediaPlayer = new MediaPlayer(mediaPlaying);
while (mediaPlaying.getDuration().toString().equalsIgnoreCase("UNKNOWN")) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PlayMgmt.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
System.out.println("Duration: "+mediaPlaying.getDuration().toMinutes());

对我来说,它有时会失败,我需要一直工作,然后我找到了一个始终有效的解决方案(到目前为止)

//get the duration of video in Seconds
public double GetVideoDuration(String pathPlaying) throws Exception{
    double DurationVideo = 0;
    IsoFile isoFile = new IsoFile(pathPlaying);
    DurationVideo = (double)
            isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
            isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
    isoFile.close();
    return DurationVideo;
}

最新更新