Loading...
Logo
Processing Forum
Hi there,

I am working on a project in which I want to play 3 video files  randomly  (each video file on each movement).

I dont know how to use Random() function.

If I use random() function , then it only repeat a 1 video at a time... but I want different video play at every time.

Can any one help me to understand how to deal with random() function...???

Regards...


By the way.. this is the sketch.... 


 
import processing.opengl.*;
import codeanticode.glgraphics.*;
import codeanticode.gsvideo.*;

GSCapture cam;
GSMovie[] movie=new GSMovie[3];
GLTexture[] tex=new GLTexture[3];
int index;
GSMovie mv;
PImage prevFrame;
int counter=0;
void setup() {
  size(640, 480, GLConstants.GLGRAPHICS);

  cam = new GSCapture(this, 640, 480);
  cam.start();


  // Use texture tex as the destination for the camera pixels.
  tex[0] = new GLTexture(this);
  tex[1] = new GLTexture(this);
  tex[2] = new GLTexture(this);

  movie[0]=new GSMovie(this, "smoke.mov");
  movie[1]=new GSMovie(this, "Drift.mov");
  movie[2]=new GSMovie(this, "Galardo.mov");


  for (int i=0;i<movie.length;i++) {
    movie[i].setPixelDest(tex[i]);
  }

  prevFrame = createImage(cam.width, cam.height, RGB);
  
  
  for(int i=movie.length-1;i>0;i--){
    int j=int(random(i+1));
    //swap
    
    mv=movie[i];
    movie[i]=movie[j];
    movie[j]=mv;
  }
}

void captureEvent(GSCapture cam) {
  cam.read();
}
void movieEvent(GSMovie movie) {

  movie.read();
}


void draw() {
  background(255);
  image(cam, 0, 0);

  MovieonMovementDisplay();

  for (int i=0;i<tex.length;i++) {
    if (tex[i].putPixelsIntoTexture()) {
      image(tex[i], 0, 0);
    }
  }
  
}
void MovieonMovementDisplay() {
  if (cam.available()) {
    prevFrame.copy(cam, 0, 0, cam.width, cam.height, 0, 0, cam.width, cam.height);
    prevFrame.updatePixels();
    cam.read();
  }


  loadPixels();
  cam.loadPixels();
  prevFrame.loadPixels();


  float totalMotion=0;

  for (int i = 0; i < cam.pixels.length; i ++ ) {
    color current = cam.pixels[i];

    color previous= prevFrame.pixels[i];

    float r1 = red(current); 
    float g1 = green(current);
    float b1 = blue(current);
    float r2 = red(previous); 
    float g2 = green(previous);
    float b2 = blue(previous);

    float diff = dist(r1, g1, b1, r2, g2, b2);
    totalMotion += diff;
  }
  float avgMotion = totalMotion / cam.pixels.length; 

  println(avgMotion+"----AvgMotion");
  println("---------");

  for (int i=0;i<movie.length;i++) {
    println(movie[i].time());
  }
      int index=constrain(movie.length,0,2);
      float time=movie[index].time();

      if((avgMotion>30)&&(avgMotion<80)){
         movie[index].play();
              
      }
      
      if(movie[index].isPlaying()){
        if(time==movie[index].time()){
          movie[int(random(0,movie.length))].jump(0);
        }
      }       
          
}


Any help must be appreciated...

Thank you... 

Replies(5)

Without deep analysis of your code, I think you want to do:
index = int(random(0,movie.length));
then use index to do the jump(0) and play() calls.
Sorry PhiLho.... for my late reply...

Actually, before posting this query ,  I have tried  the logic which you have told me.. i.e index=int(random(0,movie.length)); and use this index to do the  play() or jump() function...

But it played 2 movie together .... some times it played 3 movie together.. .

But I want only 1 movie should play Randomly from the set of movies...

Regards ...
Hi PhilLho...

in my sketch @ this line  index=int(random(0,movie.length)); random() function pick up any number .....
But in draw() function as it is continuously looping  , random() function pick up one number and at next framerate it takes another number .. Thats y some time 3 movies are running together , sometimes 2 movies are running together...

I want that on every movement different unique movie should play ( from the array of movies)...

Please help me out to solve this problem...

Regards ... 
You have to make the random choice only when you need it. Not sure when you want that, but you have to make the random number drawing to depend on an event or a state.