ofVideoPlayer playing multiple videos

Hello! I’m trying to play 4 videos using ofVideoPlayer and I’m stuck with changing the video.

I use a sensor that gives me 0,1,2 and I’ll be using these to select the videos.

but the videos must not change untill current video ends.

I successfully made video1 keep playing while the sensorvalue change, but I cant make it happen while video2 does.

I know the problem from this part

if (video1.isPlaying()) {
		store = 1;
		video2.setPaused(true);
	}
	else if(video1.isPaused()){
		store = sensorValue;
	}

	if (video2.isPlaying()) {
		store = 2;
		video1.setPaused(true);
	}
	else if (video2.isPaused()) {
		store = sensorValue;
	}

the first else if does work but the second else if works too. so that sensorValue comes into store everytime.

but I cant find the way without using isPlaying() or isPaused().

is there any method to not take new sensor values while playing the video?(applied to every video)

I’ll paste the full code

the keyboard inputs are sensor values but I’m trying it with keyboard to save time.
the sensor works fine.

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		

		ofVideoPlayer video1;
		ofVideoPlayer video2;

		ofImage startImage;

		
		int sensorValue;
		int store;
		int tempValue;
		bool first;
		bool second;
		
};
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
	video1.load("1.mp4");
	video2.load("3.mp4");
	video2.setLoopState(OF_LOOP_NONE);
	
	startImage.load("1.png");
	video1.setPaused(true);
	video2.setPaused(true);

}

//--------------------------------------------------------------
void ofApp::update(){
	if (video1.isPlaying()) {
		store = 1;
		video2.setPaused(true);
	}
	else if(video1.isPaused()){
		store = sensorValue;
	}

	if (video2.isPlaying()) {
		store = 2;
		video1.setPaused(true);
	}
	else if (video2.isPaused()) {
		store = sensorValue;
	}

	if (store == 1 && first == true) {
		video1.play();
		video1.update();
		video1.setPaused(false);
		if (video1.getPosition() == 1) {
			video1.setPaused(true);
			first = false;
			
		}
	}

	 if (store == 2 && second == true) {
		video2.play();
		video2.update();
		video2.setPaused(false);
		if (video2.getPosition() == 1) {
			video2.setPaused(true);
			second = false;
		}
	}

}

//--------------------------------------------------------------
void ofApp::draw(){
	if (store == 1) {
		video1.draw(0, 0);
	}
	if (store == 2){
		video2.draw(0, 0);
	}

	cout << "the store value is : " + ofToString(store) << endl;
	cout << "the sensor value is : " + ofToString(sensorValue) << endl;
	
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
	if (key == 'a') {
		sensorValue = 0;
		
	}
	if (key == 's') {
		sensorValue = 1;
		first = true;
	}
	if (key == 'd') {
		sensorValue = 2;
		second = true;
	}
}

I think it is, because you overwrite store when video1.isPlaying?

I would suggest to use an array of preloaded videos and set the currentVideoIndex and nextVideoIndex in the update function.

Then you may only need variables for the current Video and next Video:

if(!currentVideo.isPlaying()) currentVideoIndex = nextVideoIndex; // set index of nextVideo In Queue.

If you receive 3: set nextVideoIndex =2;

In app.h add ofVideoPlayer videos[4]; int currentVideoIndex=0; int nextVideoIndex=0; and use video[0] instead of video1…

Hey @quinton , I like pce’s idea except with a std::vector instead of an array. Arrays are fixed in size; vectors can be resized, know how many elements they contain, and have some other nice functions. An int could hold the index value of the video that is currently playing. Another int could be set from the sensor and hold the index of the next video to be played. ofVideoPlayer::getCurrentFrame() and ofVideoPlayer::getTotalNumFrames() could be compared to see if the current video is on its last frame.

So something along these lines (in psudeo-code):

// in ofApp.h
int index;
int sensorValue;
std::vector<ofVideoPlayer> videoPlayers;

// in ofApp::update()
    // the current video is playing its last frame, so time for a new video
    if(videoPlayers.at(index).getCurrentFrame() == videoPlayers.at(index).getTotalNumFrames() - 1)
    {
        videoPlayers.at(index).setPaused(true);
        // do other stuff to the old player if needed
        index = sensorValue;
        // do some other stuff to the new player, if needed
        videoPlayers.at(index).play();
    }

// then update the player
    videoPlayers.at(index).update();

// in ofApp::draw();
    // and then draw the one at index
    videoPlayers.at(index).draw(0.f, 0.f);