How to choose a movie file to play?

I wrote a simple movie player that can play and pause, but I want to be able to for the sketch to wait for a key to be pressed that would play the said movie.

So nothing happens until you press '1' and then 'movie 1' is played.

(I'm pretty new to Processing but i think i've taught myself the basics, and I suspect redraw could be used for this?)

Tagged:

Answers

  • You could just use the keyPressed() function for this. Something like:

    boolean playMovie = false;
    
    void keyPressed(){
       if(key == '1'){
          playMovie = true;
       }
    }
    
    void draw(){
       if(playMovie){
          movie.whatever();
       }
    }
    

    This is just example code, but the idea is the same: use a variable to keep track of the state of the sketch, change that state in the keyPressed() function, then use that state to determine what to do in the draw() function.

  • Check out the Input > Keyboard section of the Processing Reference. In particular, if you are planning to also unpause with the same flag, you may want to use keyReleased(), as this won't fire multiple times per press.

    boolean playMovie = false;
    
    void keyReleased(){
       if(key == '1'){ playMovie = !playMovie; }
    }
    
    void draw(){
       if(playMovie){
          movie.whatever();
       }
    }
    

    If you are planning on playing / pausing multiple movies with keys 1 2 3 4 etc... then you can duplicate this approach (10 variables, 10 tests, etc.). However, you may instead want to create a single array of flags and check them and update them with loops, e.g.

    boolean[] playButtons = new boolean[]{ false, false, false};
    

    Finally, you can use HashMap to keep a list of your keys and a true/false play button for each one. This is much more advanced to set up, but then your key checking code is extremely simple, as you only need to look up the last key pressed in the HashMap. No matter how many entries you add in setup(), the checking code just works without changing.

    /**
     * Keypress Flags with HashMap
     * -- press 1, 2, 3 to flip each between true/false.
     * 2016-09-23 Jeremy Douglass
     * Processing 3.2.1
     * https:// forum.processing.org/two/discussion/18261/how-to-choose-a-movie-file-to-play
     */
    
    import java.util.Map;
    HashMap<Character,Boolean> flags = new HashMap<Character,Boolean>();
    
    void setup(){
      flags.put('1', false);
      flags.put('2', false);
      flags.put('3', false);
      frameRate(3);
    }
    
    void keyReleased(){
      if(flags.get(key)!=null){
        flags.put(key, !(flags.get(key)));
      }
    }
    
    void draw(){
      for (Map.Entry me : flags.entrySet()) {
        if(me.getValue()==Boolean.TRUE){
          print(me.getKey(), "playing. ");
        }
      }
      println();
    }
    
  • Back in the day I followed this old sketch up on this by creating an updated one that was more a general purpose Toggle keyboard demo -- showing and tracking any keypress, and not defining them up-front. Sharing it here because it has come up again.

    /**
     * Keypress Flags with HashMap
     * -- press 1, 2, 3 to flip each between true/false.
     * 2016-09-29 Jeremy Douglass
     * Processing 3.2.1
     * https:// forum.processing.org/two/discussion/18261/how-to-choose-a-movie-file-to-play
     */
    
    import java.util.Map;
    HashMap<Character,Boolean> flags = new HashMap<Character,Boolean>();
    String flagList = "";
    String flagState = "";
    
    
    void setup(){
      //// pre-populate hashmap with lower-case alphabet
      for(int i=97; i<123; i++){ // 90/91
        flags.put((char)i, Boolean.FALSE);
        flagList = flagList + (char)i;
      }
      noLoop();
    }
    
    void keyReleased(){
      if(flags.get(key)!=null){
        flags.put(key, !(flags.get(key)));
      } else {
        flags.put(key, Boolean.TRUE);
        flagList = flagList + key;
      }
      redraw();
    }
    
    void draw(){
      flagState = "";
      for (Map.Entry me : flags.entrySet()) {
        if(me.getValue()==Boolean.TRUE){
          flagState = flagState + (char)(me.getKey());
        } else {
          flagState = flagState + '.';
        }
      }
      println(flagList);
      println(flagState);
    }
    
    //// Note testing the value is against Boolean.TRUE
    //// rather than simple "==true" or "!" tests, because
    //// Map.Entry.entrySet().getValue() returns Object, not a boolean.
    
Sign In or Register to comment.