Help add array

edited August 2016 in Questions about Code

Can someone help me incorporate a video array of up to 6 videos into this code provided? Including a void mousePressed function to click through to the next video. The way this code is written is so the video doesn't play naturally from start to finish~ so it can't be a timer function that simply plays all the videos after one another. The end result should be a basic video slideshow loop ONLY when clicked to view the next video.

import processing.video.*;

Movie mov;
int newFrame = 0;

void setup() {
  size(640, 360);
  background(0);
  // Load and set the video to play. Setting the video 
  // in play mode is needed so at least one frame is read
  // and we can get duration, size and other information from
  // the video stream. 
  mov = new Movie(this, "transit.mov");  

  // Pausing the video at the first frame. 
  mov.play();
  mov.jump(0);
  mov.pause();
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  background(0);
  image(mov, 0, 0, width, height);
  fill(255);
  text(getFrame() + " / " + (getLength() - 1), 10, 30);
  int f=ceil (map(mouseX,0,width,1,getLength()));
  setFrame(f);
}

//void keyPressed() {
//  if (key == CODED) {
//    if (keyCode == LEFT) {
//      if (0 < newFrame) newFrame--; 
//    } else if (keyCode == RIGHT) {
//      if (newFrame < getLength() - 1) newFrame++;
//    }
//  } 
//  setFrame(newFrame);  
//}

int getFrame() {    
  return ceil(mov.time() * 30) - 1;
}

void setFrame(int n) {
  mov.play();

  // The duration of a single frame:
  float frameDuration = 1.0 / mov.frameRate;

  // We move to the middle of the frame by adding 0.5:
  float where = (n + 0.5) * frameDuration; 

  // Taking into account border effects:
  float diff = mov.duration() - where;
  if (diff < 0) {
    where += diff - 0.25 * frameDuration;
  }

  mov.jump(where);
  mov.pause();  
}  

int getLength() {
  return int(mov.duration() * mov.frameRate);
}

Answers

  • Answer ✓

    could be something like this

    I don't have any movies here to test

    but the idea is to have an array of movies with an index (which movie number is current movie). In mousePressed() this gets incremented.

    FIX the NAMES of the movies to load below.

    import processing.video.*;
    
    // this is an array of movies now (a list) 
    Movie[] mov = new Movie[6];
    
    int currentMovie=0; 
    
    int newFrame = 0;
    
    void setup() {
      size(640, 360);
      background(0);
      // Load and set the video to play. Setting the video 
      // in play mode is needed so at least one frame is read
      // and we can get duration, size and other information from
      // the video stream. 
      mov[0] = new Movie(this, "transit.mov");  // FIX the NAMES!!!!!!!!!!!!
      mov[1] = new Movie(this, "transit1.mov");
    
      mov[2] = new Movie(this, "transit2.mov");
      mov[3] = new Movie(this, "transit3.mov");
    
      mov[4] = new Movie(this, "transit4.mov");
      mov[5] = new Movie(this, "transit5.mov");
    
      // Pausing the video at the first frame.
      for (int i = 0; i<6; i++) {
        if (mov[i]!=null) {
          mov[i].play();
          mov[i].jump(0);
          mov[i].pause();
        } // if 
        else
        {
          println (i+" not found!!");
        }//else
      }//for
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void draw() {
      background(0);
      image(mov[currentMovie], 0, 0, width, height);
      fill(255);
      text(getFrame() + " / " + (getLength() - 1), 10, 30);
      int f=ceil (map(mouseX, 0, width, 1, getLength()));
      setFrame(f);
    }
    
    void mousePressed() {
      currentMovie++;
      if (currentMovie>=6) { 
        currentMovie=0;
      }//if
    }
    
    //void keyPressed() {
    //  if (key == CODED) {
    //    if (keyCode == LEFT) {
    //      if (0 < newFrame) newFrame--; 
    //    } else if (keyCode == RIGHT) {
    //      if (newFrame < getLength() - 1) newFrame++;
    //    }
    //  } 
    //  setFrame(newFrame);  
    //}
    
    int getFrame() {    
      return ceil(mov[currentMovie].time() * 30) - 1;
    }
    
    void setFrame(int n) {
      mov[currentMovie].play();
    
      // The duration of a single frame:
      float frameDuration = 1.0 / mov[currentMovie].frameRate;
    
      // We move to the middle of the frame by adding 0.5:
      float where = (n + 0.5) * frameDuration; 
    
      // Taking into account border effects:
      float diff = mov[currentMovie].duration() - where;
      if (diff < 0) {
        where += diff - 0.25 * frameDuration;
      }
    
      mov[currentMovie].jump(where);
      mov[currentMovie].pause();
    }  
    
    int getLength() {
      return int(mov[currentMovie].duration() *
        mov[currentMovie].frameRate);
    }
    //
    
Sign In or Register to comment.