【问题标题】:Java, how to make program wait for video to end and then continue and wait() makes program stopJava,如何让程序等待视频结束然后继续,wait() 使程序停止
【发布时间】:2017-06-02 17:11:05
【问题描述】:

好的,我有这个方法应该运行一个视频,等到它结束,然后播放第二个视频。

我的问题是程序似乎没有等待第一个视频结束,因此直接跳转到第二个视频。

我添加了 wait() 方法,它会导致第一个视频播放,但随后它停止并且第二个视频永远不会开始。

任何帮助将不胜感激!

  private void test(ActionEvent event) throws InterruptedException {

        File fi= new File("test\\firstVideo.mp4");

        String filePath= fi.toURI().toString();

        openFile(filePath);

        wait(5000); //even wait(0) has the same effect

        fi= new File("test\\secondVideo.mp4");

        filePath= fi.toURI().toString();

        openFile(filePath);
    }

【问题讨论】:

  • 我没有实现它,我只是写了 wait(5000)

标签: java video delay wait


【解决方案1】:

我认为你混淆了 wait() 和 sleep() (Difference between wait() and sleep())

在你的情况下,你可能想听一些文件/视频事件并在那里处理它。

【讨论】:

  • 我试过睡眠,它让程序等待设置的时间,然后它只播放第二个视频:S 你能解释一下视频收听部分吗?
  • 你能给我更多关于你的openFile函数的细节吗?
  • private void openFile(String filePath){ if (filePath != null) { Media media = new Media(filePath); mediaPlayer = new MediaPlayer(media); mediaView.setMediaPlayer(mediaPlayer); DoubleProperty width = mediaView.fitWidthProperty(); DoubleProperty height = mediaView.fitHeightProperty(); width.bind(Bindings.selectDouble(mediaView.sceneProperty(), "width")); height.bind(Bindings.selectDouble(mediaView.sceneProperty(), "height")); mediaPlayer.play(); } }
【解决方案2】:

你能得到视频的长度吗?因为 Java 能够读取某些文件的元数据...您可以尝试使用计时器来代替等待视频完成。也许您可以获取视频的长度并设置一个计时器以使用不同的参数调用相同的函数(递归)并播放另一个视频。所以基本上使函数以路径为参数,并播放该参数的视频。如果您还不知道,Java 有一个 Timer 类,其中一个计时器实例需要一定的秒数,并在其构造函数中调用一个函数,并且它有一个 .play() 方法。

【讨论】:

  • 非常感谢,这正是我所需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多