Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+33 votes

We are getting a total crash (program closes) and a weird error from opencv when we tried to have the video play automatically (using the timer thread) AND also have slider bar to choose the position/frame.

How can we avoid this crashing behavior?

asked in CSC285_Fall2018 by (2.2k points)

1 Answer

+13 votes
 
Best answer

It turns out that the issue is that the opencv for Java library is NOT "thread safe", so it's bad if multiple threads of Java code both try to interact with the VideoCapture object at the same time.

To avoid this, you need to add both of these lines inside the event handling code for the slider (when the changed event happens), in order to shut down the timer's thread (and wait for it to finish) whenever the scrollbar is used.

timer.shutdown();  // stop the auto-playing
timer.awaitTermination(1000, TimeUnit.MILLISECONDS);
answered by (1.2k points)
selected by
...