Processing’s Video library doesn’t offer any means to flag a Movie stream has ended.
A long time ago I’ve posted a hack solution that used to work back then:
But more recent changes to the library made that hackish workaround to stop working.
This time around I’ve decided to create a HackedMovie subclass which extends
the Movie class:
It features a new boolean
field ended which is set to true
when a Movie stream has finished.
Also a new callback eosEvent(), in addition to movieEvent(), which is invoked right after a Movie stream has finished.
More EoS listeners can be added via new method HackedMovie::addEOSListener().
Just add the “HackedMovie.java” file to your sketch and import
it like this:
import processing.video.HackedMovie;
In short, HackedMovie subclass is used as a replacement to the Movie class.
P.S.: That “.java” file relies on the original video library, so you have to install it so subclass HackedMovie can work.
Important update (v2.1.0):
Callback eosEvent() requires now a Movie or HackedMovie parameter, just like movieEvent():
“HackedMovie.java”:
/**
* Hacked Movie EoS Event (v2.1.0)
* by GoToLoop (2022-Oct-20)
*
* Gist.GitHub.com/GoToLoop/67023ba3e9ba1d0301c40f7c030d176a
*
* Discourse.Processing.org/t/hacked-movie-eos-event-library/39375
* Forum.Processing.org/two/discussion/14990/movie-begin-and-end-events#Item_1
*/
package processing.video;
import processing.core.PApplet;
import java.lang.reflect.Method;
import org.freedesktop.gstreamer.GstObject;
import static org.freedesktop.gstreamer.Bus.EOS;
public class HackedMovie extends Movie {
public Method eosEventMethod;
public boolean ended;
public HackedMovie(final PApplet p, final String filename) {
super(p, filename);
addEOSListener(new MyEOS()).lookUpAndActivateEOSEventCallback();
}
public HackedMovie addEOSListener(final EOS eos) {
playbin.getBus().connect(eos);
return this;
}
public HackedMovie lookUpAndActivateEOSEventCallback() {
final Class<?> p = eventHandler.getClass();
try {
eosEventMethod = p.getMethod("eosEvent", Movie.class);
}
catch (final NoSuchMethodException e) {
}
try {
eosEventMethod = p.getMethod("eosEvent", HackedMovie.class);
}
catch (final NoSuchMethodException e) {
}
return this;
}
@Override public void setEventHandlerObject(final Object o) {
super.setEventHandlerObject(o);
final Class<?> p = o.getClass();
try {
movieEventMethod = p.getMethod("movieEvent", HackedMovie.class);
}
catch (final NoSuchMethodException e) {
}
}
@Override public void play() {
super.play();
ended = false;
}
class MyEOS implements EOS {
@Override public void endOfStream(final GstObject _) {
ended = true;
if (eosEventMethod != null) try {
eosEventMethod.invoke(eventHandler, HackedMovie.this);
}
catch (final ReflectiveOperationException e) {
}
}
}
}