Multi play ofVideoPlayer clones

I am trying to play a single video multiple times (for exemple each time a key is pressed) and at random <X;Y> positions…
That much I can do.

the new ofVideoPlayer clone must restart the video. and I realized that all ofVideoPlayer clones are playing each video at the same position. which means that even if I press keys with delay all videos will end simultaneously.

this is my code so far


ofVideoPlayer mainPlayer;
vector<ofVideoPlayer> players;


void ofApp::setup() {

void ofApp::update(){
    int id = 0;
    for (auto &player : players) {
        if (player.isPlaying()) {
        } else {

void ofApp::draw(){
    //Draw all players
    for (auto &player : players) {
        player.draw(randomX, randomX, width-randomW, height-randomH);

void ofApp::keyPressed(int key){
    ofVideoPlayer playerClone;
    //Also tried with same results : playerClone = player;;

Thanks a lot for your help

you shouldn’t clone the video player like that just create a new one by loading the movie again. What you are doing there is just creating a new reference to the original.

instead of:


just do:


Thanks @arturo for that solution

I’m trying to learn optimisation coding and I feel like loading the file again and again will challenge my CPU

I am now looking into loading the file once into an ofFile and then pass that file to ofVideoPlayer.load() could be another solution :slight_smile:

That won’t make any difference. The file isn’t really loaded all at once so the only difference between having only one file opened would be minimal, only the number of file handles would change which is really not important in something like this

if the video is short you could make a copy with a different name and load all the dir containing the videos into a vector at the setup

1 Like

I don’t know how many times the keys will be pressed so I need to load dynamically :confused:

The only thing that can be slower in something like this is that the initial loading can be slower than using a video that is already loaded.

One way to alleviate the problem is to use loadAsync instead of load to avoid at least blocking the main thread and possible stuttering if the block takes longer than one frame

Another way if that’s not enough is to have a pool of video grabbers as crystem suggests. Even if you don’t know how many times the key is going to be pressed there’ll be a maximum that makes sense, mostly cause more than that will be to much to handle in terms of performance but even in terms of how much you can show in the screen. So you ca just preload as many videos and not update them until they are active. Once you have used all of them just reuse the first one that was activated for the new one

In any case don’t try to optimize until it’s really needed, if just calling load or loadAsync is fast enough trying to optimize it further will only make your project more complicated and error prone