We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! In my project I have to play a string of videos randomly and at the same time, the problem is that very time a new video is added the others, which are already playing, freeze for a second! I´ve read that preloading them could fix this problem but i don´t now how to go about it and if it realy works?
I will live the code i´m working on here hope you can help me out!
// libs:
import processing.video.\*;
// declarations
int $winW = 1920; // window
int $winH = 1080;
int $margin = 20;
int $celsW = 20; // numero de celulas horizontal
int $celsH = 15; // numero de celulas vertical
int $celW, $celH;
int $fps = 25;
int $beat = 10; // tempo entre cada video!
//lista de videos
String[] $videoFiles = {
"1.mov",
"2.mov"
};
int $frame = 0;
int $vidMax;
Movie[] $videos;
/\*
\* Main proggy
\*/
//
// SETUP:
//
void setup() {
size(1920, 1080);
background(0);
frameRate($fps);
// defenir o tamanho de cada celula
$celW = $winW/$celsW;
$celH = $celW;
/\* Init matrix \*/
$videos = new Movie[$celsW\*$celsH];
$vidMax = $videoFiles.length;
}
//
// DRAW:
//
void draw() {
background(0);
// show something new?
if ($frame % $beat == 0) {
int vid = floor(random(0, $vidMax));
int pos = floor(random(0, $videos.length));
while ($videos[pos] != null) {
pos = floor(random(0, $videos.length));
}
$videos[pos] = new Movie(this, $videoFiles[vid]);
$videos[pos].jump(0);
$videos[pos].noLoop();
$videos[pos].play();
println("loading video " + vid + " at position " + pos);
}
// read videos:
for (int i = 0; i < $videos.length; i++) {
if ($videos[i] != null) {
if ($videos[i].time() < $videos[i].duration() - 0.1) {
$videos[i].read();
int x = (i % $celsW) \* $celW;
int y = (i / $celsW) \* ($celH+$celsH/3);
// int y = (i / ($celsH+$celsH/2)) \* ($celH+$celsH/2);
image($videos[i], x, y, $celW, $celH);
} else {
$videos[i] = null;
}
}
}
$frame++;
}
Answers
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
Thanks! i was trying to fix that, the text was terrible!!!
I´ve taken a look to want you sent and it´s fixed now!
Hey GoToLoop! I was reading some of your post and i found one where you solve the problem of playing 2 videos at the same time! My teacher told me the solution would be for me to preload the videos, for what i´ve read it would have to be on the void setup. However I having some trouble finding a way to preload a video?
Hi Tiago, I still couldn't pinpoint exactly what you're asking?
Are you playing all your loaded videos at once?
Or just 1, then grab another random vid once finished?
Or is it a mix of both? If so, exactly how?
Indeed, we should strive to load all resources within setup() if it's feasible.
And lastly: Why are you prefixing all your variables w/
$
anyways? ^#(^I will try to explain what my code is doing but you can downlaod this zip file and have a look, that way you will understand better. Zipfile:http://we.tl/gZ71ihLEEF
What i´m trying to accomplish is to have a grid of squares where randomly a video is playing or not, they could be playing at the same time or just one. It´s like the windows of a building at night. The problem is that when a new video is added the others freeze for a second!!
hope I explained it better!
Hey! In this discussion :https://forum.processing.org/two/discussion/comment/57830/#Comment_57830
I manage to finaly find something that might be what i´m looking for
GotoLoop do you think i can adapt this to preload around 70 videos? Or is there a better way!?
I've just looked at your "sketch_4.pde" sample and things don't look too easy! :-S
Even though you're using only 2 small files: "1.mov" & "2.mov", you're still creating lotsa unique Movie instances outta them! @-)
This type of approach makes unfeasible to instantiate everything within setup(). :-<
It all depends whether it is allowed for you to use the same Movie instance across "cells".
That is, whether you can play() the same Movie showing the same frame position for all "cells".
Only then you can load everything within setup().
Otherwise, you're gonna need to come up w/ some very complicated approach to create Movie instances on-the-fly after setup()! 8-|
Sorry GoToLoop, but as I´m a begginer at this, I don´t know what you mean by "movie instances"!? :( And what would it be the problem of using the same movie instance?
Instance of some class = object. :P
If you use the same Movie instance, every cell displaying it gonna show the same video position.
In other words, you won't be able to have 1 cell showing the beginning and another 1 showing some other part of the same Movie. All of them is gonna show the same thing at the same time! ~:>
-.- if I did what you are subjecting I wouldn't be able to have 1.mov and 2.mov playing at the same time in different cells! That wont work :( I wouldn't mind if 1.mov would had is own movie instance and if a cell was playing that video no other could do it!
I´ve tried to export the video in several ways to see if the size or codec of the video was the problem but i didn't had any luck X(
In you opinion what could be causing this lag on starting the videos? Since no video is added I can have 10 videos playing without any problem!
You misunderstood me. You can have as many of them playing in various cells at the same time.
The only impediment is that they would show the same scene frame for all cells.
Any loading and save operations cause lag!
That's why they should happen within setup() before draw() starts.
Loading & saving stuff once draw() had started w/o causing lag to it demands advanced techniques.
Ups! my bad #-o So if a video was already playing and another was added it would play the frame equivalent to the other which was already playing.
Thanks for that!
I was wondering!! :-? As you said i have to load the videos on setup to avoid lag but having so many movie instances it´s a problem! (i believe you since i could not grasp why that happens :P ) so i thought, could we create like 5 movie instances width 14 videos for each!? i hope i´m going some where width this :P
You'll load and create as many Movie objects as your total number of video files within setup().
It's got nothing to do w/ your actual number of "cells".
Then each cell's gonna choose which Movie to play() among those loaded within setup().
That is, they won't create any more Movie objects after setup(). Just gonna re-use them.
ok! i think that is it! For me it would be perfect not to have multiple "cells" playing the same video! Sorry for taking so long to get it and THANK YOU! ^:)^ Now i will just have to try to do it :-S
Hello GoToLoop! I`ve been working on the code and the lag is resolved! But i still have the same video playing simultaneously, which causes some wierd freezes!
What i was trying to do is to go through a array i created and check if theres is any video already playing !! if so the vid varibel would run again and so on...
I found the code below on the net and i was trying to adapt it so that i could use it
this is the code from my project
is this the best way?!
Problem solved!! I will leave here the code for anyone o might have the same problem!
/* * Boilerplate */