1

I am using SikuliX to check when a video on a website has ended.

I do this by comparing my region (which is my active web browser window) to a screen capture of the region I have taken while the video is playing.

If it doesn't match, that means the video is still playing and I will take a new screen capture, which will be run through the while loop again for comparison. If it matches, it means the video has stopped and will exit the while loop.

It works when it first goes through the loop. The while loop returns null which means the video is playing. However, on the second time it loops, it will exit the while loop and tell me my video has stopped but it clearly hasn't.

Is my logic flawed?

// 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");
Csh
  • 103
  • 1
  • 11

1 Answers1

0

Instead of using while (region.exists(p.similar((float) 0.99), 0) == null), using while (region.compare(image).getScore() == 1.0) to compare the region with the screen capture gave the results I wanted.

Csh
  • 103
  • 1
  • 11