检查视频是否结束

Check if video has ended

我正在使用 SikuliX 检查网站上的视频何时结束。

我通过将我的区域(这是我的活动网络浏览器 window)与我在播放视频时拍摄的区域的屏幕截图进行比较来做到这一点。

如果不匹配,说明视频还在播放,我会重新截屏,运行再次通过while循环进行比较。 如果匹配,则表示视频已停止,将退出while循环。

它在第一次通过循环时起作用。 while 循环 returns null 表示视频正在播放。然而,在第二次循环时,它会退出 while 循环并告诉我我的视频已停止但显然没有。

我的逻辑有问题吗?

// Get dimensions of the bounding rectangle of the specified window
WinDef.HWND hwnd = User32.INSTANCE.GetForegroundWindow();
WinDef.RECT dimensions = new WinDef.RECT();

// Get screen coordinates of upper-left and lower-right corners of the window in dimensions
User32.INSTANCE.GetWindowRect(hwnd, dimensions);

Rectangle window = new Rectangle(dimensions.toRectangle());

int x = window.x;
int y = window.y;
int width = window.width;
int height = window.height;

// Initialize screen region for Sikuli to match
Region region = new Region(x, y, width, height);

Robot robot;
Image image;
Pattern p;

try {
    robot = new Robot(); // Gets and saves a reference to a new Robot object 
} catch (AWTException e) {
    throw new RuntimeException(
        "Failed to initialize robot..."); 
}

robot.delay(3000); // Delay robot for 3 seconds 

// Take a screen capture of the region
BufferedImage capture = robot.createScreenCapture(dimensions.toRectangle());
image = new Image(capture);
p = new Pattern(image);

region.wait(1.0); // Wait 1 second

// Check if region content is still the same
while (region.exists(p.similar((float) 0.99), 0) == null) {
    System.out.println("Video is playing");

    // Take a new screen capture of the region
    BufferedImage captureLoop = robot.createScreenCapture(dimensions.toRectangle());
    image = new Image(captureLoop);
    p = new Pattern(image);

    region.wait(1.0); // Wait 1 second
}   

System.out.println("Video has stopped");

而不是使用 while (region.exists(p.similar((float) 0.99), 0) == null),使用 while (region.compare(image).getScore() == 1.0) 将区域与屏幕截图进行比较给出了我想要的结果。