Playing Video from a parallel thread


#1

I am writing a program where a thread passes over a flag and a path through a global variable

Part of the thread class

void play()
	{
		globalpath = path;
		cout <<"playing_"<<globalpath<<endl;
		playcheck = true;
		while(playcheck==true)
		{ } //freezes the thread while video is playing

	}

Part of ofApp.cpp

void ofApp::draw(){
	if (playcheck == true )
	{
		playvideo(globalpath);
		playcheck == false;
	};
}

void ofApp::playvideo(string path)
{

	player.load(path + ".mp4");
	player.play();
	while (player.getCurrentFrame() < player.getTotalNumFrames())
	{
		player.nextFrame();
		player.draw(0,0);
	}
}

When I execute the code, the display window is painfully slow and takes a lot of time to load the first frame, and then the window freezes ( crippling speed )

Is there a better way to execute the same?
I want to execute a video playback from the thread and wait till the playback is over


#2

why do you need to do it on a different thread?
Anyways, usually the video player will run internally on a different thread, if you are doing it for performance reasons.
As for the slow playback, that while loop in playvideo doesnt look like a good idea, as the draw function will get called at the monitor refresh rate, typically 60 times per second, so whatever code is executed in draw should take a 1/60 of a second to be able to run at such framerate. If it takes longer, as in your code, it will draw super slow.
Follow the video player examples.
BTW, what OS and IDE are you using? if you are on windows, running video playback is super slow on debug mode.


#3

Thanks a lot, I fixed it, I made a typo playcheck == false; instead of playcheck = false;
Also moved

if (playcheck == true )
{
	playvideo(globalpath);
	playcheck == false;
        player.load(path + ".mp4");
        player.play();

};

into update()

and the draw() is just

	if (player.getCurrentFrame() < player.getTotalNumFrames())
{
	player.update();
	player.draw(0,0);
}

#4

It looks wrong. as the == operator is for making comparisons, so this like actually has no use, and it looks like playcheck never changes it’s state.