I could open it fine and it looks like you’ve got the basic framework in place!
I think you’re problem is in the timing of events. When you say var vid = videos[Math.floor(Math.random() * videos.length)];
that line will run before preload, setup, and draw. Which means that it can’t find any videos in the videos array so vid will be set to undefined. I can’t say for sure but I think that’s what’s causing your problem.
I would suggest reducing the number of variables you have. There are some times putting the same object in to multiple variables makes sense but I’ve found that it can lead to situations like this where it’s unclear which variables are which things. Hope that makes sense.
Anyways it seems like you only need two variables an array for the videos and one to store a number of which video you’re currently using.
var videos = [];
var currentVideoIndex; // this could be shorter but want to make it clear
Then your preload could look something like:
function preload() {
videos = [createVideo('vid0.mp4'), createVideo('vid1.mp4'), createVideo('vid2.mp4')];
}
This way you won’t have a bunch of different variables to keep track of and you can think of videos[0], videos[1], videos[2]
as replacing vid0, vid1, vid2
.
Remember to set currentVideoIndex
after preload but before draw (so in setup). Like:
currentVideoIndex = Math.floor(Math.random() * videos.length);
// or you can use the p5 functions like
currentVideoIndex = floor(random(videos.length));
After all that image(videos[currentVideoIndex], 0, 0, 640, 480);
should work.
There’s one other thing that maybe causing the error. When you use playNextVideo()
in draw that function is getting called every frame because draw loops forever. This means the video will switch every frame. I would move it into setup to avoid this problem.
The last thing that’s unrelated to the code working is that you might notice that all the videos will still show up on the page in addition to the canvas. This is because createVideo()
actually creates a <video>
html tag. To hide these you can use .hide() on a video variable. Like:
videos[0].hide();
Hope that helps! Let me know if anything was confusing.