play a random list of videos by keypress, exhaust

edited April 2017 in Library Questions

I need help please, how could make according to press a key "1" play in random select from a list of videos, while its pressed? (the video start playing on loop, so that the list is just a video. Not play the next, until you release the key (else background = 0) or (stop all), when press key "1" (or 2) again then random looks again in "one" list).

--any ideas?, is not important for my use array-- code:

import processing.video.*;

String[] one = {"1.mov","2.mov","3.mov"};
String[] two = {"4.mov","5.mov","6.mov"};
String[] three = {"7.mov","8.mov","9.mov"};

Movie myMovie; //Movie

int a = 0; 
float md = 0;
float mt = 0;

void setup() {
 size(400, 400);
}
void draw(){
 if (keyPressed) {
    if (key == '1'  {
      for (int i = 1; i < myMovie.length; i ++ ) {    myMovies[i] = new Movie(this, one);
      myMovies[i].loop();
    } 
    }
  } else  background(0);
//////////////////////////
  if (keyPressed) {
    if (key == '2'  {
      for (int i = 1; i < myMovie.length; i ++ ) {    myMovies[i] = new Movie(this, two);
      myMovies[i].loop();
    } 
    }
  } else  background(0);
/////////////////////////////
 if (keyPressed) {
    if (key == '3'  {
      for (int i = 1; i < myMovie.length; i ++ ) {    myMovies[i] = new Movie(this, three);
      myMovies[i].loop();
    } 
    }
  } else  background(0);
 //myMovie = new Movie(this, one[int(random(i.length))]);
    // myMovie.play();
    } 

Answers

  • sorry i am a new user

  • Answer ✓

    Highlight code text and hit CTRL+K in order to format it for the forum!
    http://forum.processing.org/two/discussion/6570/multiple-key-presses

  • well, its good to play keys, but how can take random video of a list, when pressed a key. I'm not yet resolved. code: import processing.video.*;

    String[] movieNames = { "1.mov", "2.mov", "3.mov" };
    Movie[] theMov;
    boolean isPlaying;
    boolean isLooping;
    
    void setup() { 
      size(640, 480);
    
      theMov = new Movie[movieNames.length];
    
      //theMov.loop();  //plays the movie over and over
      isPlaying = true;
      isLooping = true;
    }
    
    void draw() { 
      background(0);
      //image(theMov,0, 0);
       image(theMov[int(random(movieNames.length))]);//here is error
    } 
    
    void movieEvent(Movie m) { 
      m.read(); 
    } 
    
    void keyPressed() {
      if (key == 'p') {
        // toggle pausing
        if (isPlaying) {
         image(theMov[int(random(movieNames.length))],0,0);
        }     
          } 
    } 
    
  • Answer ✓

    You did a good move by going from the keyPressed variable in draw() to the keyPressed() function. You probably also need to use the keyReleased() function.

    I rarely use (if ever) the video library, but I believe you have to use .play() on the movie you want to see. Perhaps it is the source of the error you point to.

    BTW, "here is error" is useful, but it would be even better to copy & paste the error message in the forum, so we get an idea... (we often cannot run the code shown, sometime lacking the libraries, or just lacking the assets).

  • edited August 2014 Answer ✓

    Only the Movie[] array is instantiated. I can't spot any movie loading anywhere and assigned to some array's slot!!!

  • Answer ✓

    Yes, it was done in the first version of the code, but omitted in the second one...

  • edited August 2014

    oh sorry Philho that's what I'll do. this it's the first line: import processing.video.*;

    you know

  • Gotoloop, all videos are in data folder and his names are: 1,2,3...

  • As mentioned, you don't load those 1, 2, 3 movie files at all in the example you provided! /:)
    You've merely instantiated an array, but its "slots" are still null empty! :-S

    http://processing.org/reference/Array.html

  • edited August 2014

    sorry my brain no anwser, :-O , i don't know how put the array and radom guys that's it all:

            import processing.video.*;
            String[] moviesNames = { "1.mov", "2.mov", "3.mov","4.mov" };
            int index = int(random(moviesNames.length));
            Movie[] movies;
            //Movie mov_a,mov_b,mov_c,nowPlaying;
    
    
            void setup(){
              size(640,480,P2D);
              background(0);
              movies = new Movie[moviesNames.length];
            for (int i = 0; i < moviesNames.length; i++) {
              movies[i] = new Movie(this, moviesNames[i]);
            } 
    
              //mov_a; = new Movie(this, "5.mov");
             // mov_b; = new Movie(this, "6.mov");
             // mov_c; = new Movie(this, "7.mov");
             nowPlaying = mov_c;
             nowPlaying.loop();
            }
    
            void draw(){
              background(0);
              image(nowPlaying,0,0,width,height);
    
    
            }
    
    
            void movieEvent(Movie _mov){
              _mov.read();
            }
    
            void keyPressed(){
    
              if (key == 'a'){
                nowPlaying = mov_a;
                //nowPlaying.jump(0);
                nowPlaying.loop();
              }
              if (key == 'b'){
                nowPlaying = mov_b;
                //nowPlaying.jump(0);
                nowPlaying.loop();
              }
                if (key == 'x'){
                nowPlaying = movies;
                //nowPlaying.jump(0);
                nowPlaying.loop();
              }
              }
    
  • Variable nowPlaying doesn't exist! Actually, it isn't even necessary b/c you already got an index variable! 8-X
    For example @ your line #25 rather than nowPlaying, you coulda used your array w/ current index:
    image(movies[index], 0, 0, width, height);

    Another tip: Inside Processing's IDE, hit CTRL+T in order to auto-format the code! ;;)

  • edited August 2014

    Fantastic man, thank you :-* [-O<
    but please, i have 2 problems: 1-random its good but only the first time run, need run again to load a new diferent video. (will be good change push other key or released key or goto firstline)

    2-how can i go to black screen when push "z" and load a new video random when push q again? (this last could be resolve problems 1 )

    import processing.video.*;
    String[] moviesNames = { 
      "1.mov", "2.mov", "3.mov", "4.mov", "5.mov", "6.mov"
    };
    int index = int(random(moviesNames.length));
    
    Movie[] movies;
    Movie mov_a, mov_b, mov_c, nowPlaying;
    
    
    void setup() {
      size(640, 480, P2D);
      background(0);
      movies = new Movie[moviesNames.length];
      for (int i = 0; i < moviesNames.length; i++) {
        movies[i] = new Movie(this, moviesNames[i]);
      }
    }
    
    void draw() {
      background(0);
      // image(nowPlaying,0,0,width,height);
      image(movies[index], 0, 0, width, height);
    }
    
    
    void movieEvent(Movie _mov) {
      _mov.read();
    }
    
    void keyPressed() {
    
      if (key == 'q') {
        nowPlaying = movies[index];
        //nowPlaying.jump(0);
        nowPlaying.loop();
      }
      else nowPlaying.stop();
      int index = int(random(moviesNames.length));
    } 
    
  • edited August 2014

    As I had pointed at my previous answer, you were already using variable index as a way to track down which Movie is currently being played!
    Variables mov_a, mov_b, mov_c, nowPlaying are completely redundant!
    Either go w/ index or nowPlaying, not both approaches! [-X

    • Following the index approach, within keyPressed(), pause() current index Movie.
    • Then pick up a diff. random() value for index.
    • Finally, loop() that newly chosen index Movie.
  • edited August 2014

    jajajajajaj :))
    i love it [-X

  • edited August 2014

    i can eliminate a, b and c, and change index for nowplaying, but I do not know how to do yours 3 points, X_X , please write me lines 8-}

  • edited August 2014

    ·Following the index approach, within keyPressed(), pause() current index Movie. where go pause? delete "keyPressed"?

    ·Then pick up a diff. random() value for index. I don't understand, sorry (i'm spanish EU)

    ·Finally, loop() that chosen index Movie. I don't know how to do. My knowledge have a limits. thanks over there :o3 :o3 :o3

  • edited August 2014

    Something like this: *-:)

    // forum.processing.org/two/discussion/6590/
    // play-a-random-list-of-videos-by-keypress-exhaust
    
    void keyPressed() {
      int k = keyCode;
      if (k == 'Q')  pickRandomVideoIndex();
    }
    
    void pickRandomVideoIndex() {
      if (movies.length <= 1)  return;
    
      movies[index].pause(); // pause current video.
    
      int rnd; // keep picking a new index till got a diff. 1:
      while ( (rnd = (int) random(movies.length)) == index );
    
      // assign newly picked random value to index:
      movies[index = rnd].loop(); // and start playing it.
    }
    
  • edited August 2014

    thank you for your present, you are a brilliant =D>
    now i learn a "little bit" thank you to you

  • Hello my friends :)

    I use coding here and I have a little problem. I added command movies[index].play(); and my video plays. When it's done I see last frame. Correct.

    void draw() {
      background(0);
      // image(nowPlaying,0,0,width,height);
      image(movies[index], 0, 0, width, height);
     movies[index].play();
    }
    

    But when I put Q and playing other video, the second video makes playing and when it's done, it makes loop. How can stop loop second video.

  • Could you provide all your code, or at least a minimum amount of code showing your approach? In the snippet that you provided, it is not a good idea to call line 5 in draw. You see, you are calling that line 60 times a second [-(

    Kf

  • Hello,

    As it stands this code pics one randomised image from my data folder, whenever a capacitive sensor is held down.

    I am wanting to replace these images with video files however, I am not sure how to do this.

    Any help would be greatly appreciated!

    import processing.serial.*;
    
    int threshold = 30; 
    PImage [] picArray = new PImage [26]; 
    Serial myPort;
    
    boolean holdImage=false;
    int imgIndex;
    
    void setup() {
      size (500, 500);
    
      for (int i=0; i<picArray.length; i++)
        picArray[i]=loadImage(i + ".png");
    
      myPort = new Serial(this, Serial.list()[1], 9600);
      myPort.bufferUntil('\n');
    }
    
    
    void draw() {
    
      background (255, 255, 255); 
      if (holdImage==true) { 
        image(picArray[imgIndex], 0,0);
      }
    }
    
    
    void serialEvent(Serial myPort) {
    
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null) {
    
        inString = trim (inString);
        float[] touches = float (split(inString, ","));
    
        if (touches.length >=1) {
          int touch1Value = touches[0] >= threshold ? 1: 0;
    
          if (touch1Value == 1) {
            if (holdImage == false) {
              imgIndex=int(random(picArray.length));
              holdImage=true;
            }
          } else
            holdImage=false;  //RELEASE holder so to detect next usr press event
        }
      }
    }
    
  • please stop posting the same code all over the forum.

    you have a dedicated thread for this, what do you think you'll gain by resurrecting another thread from 2014?

This discussion has been closed.