Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Changing which video is playing on button press

$
0
0
Hello,

I currently have a sketch here, that runs a video in a circle that follows the mouse. I would like to get it so that the video changes when a button is pressed.

I have it that the video changes while one is holding the button, but i want one press to equal a sort of "play" button, i will have multiple buttons all wired to a different video.

This code also seems to run really slow, any ideas on how to improve that aspect are welcome as well.

thanks

  1. import processing.video.*;
    import processing.serial.*;

    Serial myPort;  // Create object from Serial class
    int val;      // Data received from the serial port
    Movie movie1, movie2, middlemovie;

    void setup() {
      size(displayWidth, displayHeight);
      background(0);
      frameRate(30);
      // Load and play the video in a loop
      movie2 = new Movie(this, "looping_static_1.mov.1");
      movie2.loop();
      String portName = Serial.list()[0];
      myPort = new Serial(this, portName, 9600);
    }

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

    void draw(){
      if ( myPort.available() > 0) {  // If data is available,
        val = myPort.read();         // read it and store it in val
      }
      if (val == 0) {             // If the serial value is 0,
        movie1 = new Movie(this, "transit.mov"); 
        movie1.loop();  
      }
      else {                      
        movie1 = new Movie(this, "looping_static_1.mov.1");
        movie1.loop();  
      }
      float solidRadius = 160.0; // radius of solid circle.
      float radius = 200.0; // radius of the fading circle.
      movie1.read();
      movie2.read();
      // movie1.loadPixels();
      image(movie2, 0,0);
      loadPixels(); 
      for (int x = mouseX-int(radius); x <= mouseX+int(radius); x++ ) {
        for (int y = mouseY-int(radius); y <= mouseY+int(radius); y++ ) {
          if( y < 0 || y >= height || x < 0 || x >= width ){
            continue;
          }
          int loc = x + y*width;
          // if this next line gives you issues, use the commented out one below it instead. 
          color val = movie1.get(x,y);
          // color val = movie1.pixels[loc];
          // And also uncomment the line "movie1.loadPixels();", above.
          float distance = dist(x,y,mouseX,mouseY);
          float transparency = map(radius-(distance-solidRadius), solidRadius, radius, 0.0, 255.0);
          color c = color(red(val), green(val), blue(val), transparency );
          pixels[loc] = distance<radius?blendColor(pixels[loc],c,BLEND):pixels[loc];
          pixels[loc] = distance<solidRadius?val:pixels[loc];
        }
      }
      updatePixels(); 
    }

Viewing all articles
Browse latest Browse all 1768

Trending Articles