Problem using Video in Objects/class

edited April 2016 in Library Questions

Hello, I am trying to use video inside an object. This code worked fine before I started building the class - I think my problem is that I don't know where to place the initialisation of the movie. Below is my code and I am getting an error on line 43. The error says " The constructor "Movie(Tile,String)" does not exist". Eventually i would like to have a number of tile objects arranged in a certain pattern and when the cursor moves over each tile it plays the movie file and plays a separate sample.

Thanks for the help (code below)

    import processing.video.*;
    import ddf.minim.*;
    Movie movie1; 
    Minim minim;
    AudioSample sample;  

    Tile t1;                

    void setup() {
    fullScreen(2);

    }


     void draw() {

    background(155);
    t1.presentTile(400,500);


 }

 class Tile {


    int boxSize;
    boolean overBox = false;
    boolean canTrigger = true;
    float boxMax = 100;
    float boxMin = 25;

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

Tile() {


    imageMode(CENTER);
    boxSize = 25;

    minim = new Minim(this);
    movie1 = new Movie(this,"TORCH_2_1.mov");
    movie1.loop();
    movie1.pause();
    sample = minim.loadSample("Paper Rustle_1.mp3");

    t1 = new Tile(); 



}



void presentTile(float bx,float by) {

    if (mouseX > bx-(boxSize/2) && mouseX < bx+(boxSize/2) &&  
    mouseY > by-(boxSize/2) && mouseY < by+(boxSize/2)) {
    overBox = true; 

    } else {

    overBox = false;
    }

    if ((overBox) && (boxSize < boxMax)) {

    image(movie1, bx, by,boxSize++,boxSize++);
    movie1.loop();

    }
    if ((!overBox)&&(boxSize > boxMin)){

    image(movie1, bx, by,boxSize-=3,boxSize-=3);
    movie1.pause();

    }
    image(movie1,bx,by,boxSize,boxSize);

    if (canTrigger && overBox) {
    sample.trigger();
    canTrigger = false;

    }

    if (!overBox) {
    sample.stop();
    canTrigger = true;              
    }

}

}

Answers

  • For a start line 48 must not be in the Tile constructor it should be in setup ().

    Why have you created the Tile class? Are you going to have many Tiles? What does a Tile represent?

  • Eventually I would like to have a number of tiles arranged in a certain pattern and when the cursor moves over each tile it plays the movie file and plays a separate sample. Each tile will be the same but only be activated when the mouse cursor hovers over it. I tried just using a tile function, as all i really need to do is produce a number of tiles in different locations but then I ran in to a problem with the cursor no longer detecting each tile - I assumed because each tile needs to be a new instance with its own variables . Hence why I decided to try make the tile an object class.

    Yes i see that mistake with t1 = new Tile(); now, thanks for the help. Any help with the problem on line 43 would be greatly appreciated. I don't understand were that code should sit when I make a class. - as I said - in an earlier sketch I had this code working fine when there was just one instance of a tile.

    thanks again!

  • Thanks for the links - will defo be useful!

    Perhaps its best I show the sketch I made 1st before I tried making many instances of what I call a tile. As I said this sketch below works fine - the video and sound play when the cursor moves over the movie box/tile (whatever you want to call it) and the tile enlarges. When the cursor moves away from the tile the video/sound stops and it returns to its original size.

    All good.

    The problem is I can't seem to create a class from this. Another way to phrase the question I guess would be - how do I take the below code (which works) and create multiple instances of this tile/video box on the screen in different locations.

    Thanks again for the help!

    (below is code of the sketch before i tried to make a class etc)

        import processing.video.*;
        import ddf.minim.*;
    
    
        Movie movie1; 
        Minim minim;
        AudioSample sample;
    
        float bx;
        float by;
        int boxSize;
        boolean overBox = false;
        boolean canTrigger = true;
        float boxMax = 100;
        float boxMin = 25;
    
        void setup() {
        fullScreen(2);
        minim = new Minim(this);
        bx = width/2;
        by = height/2;
        imageMode(CENTER);
        boxSize = 25;
    
        movie1 = new Movie(this, "TORCH_2_1.mov");
        movie1.loop();
        movie1.pause();
        sample = minim.loadSample("Paper Rustle_1.mp3");
        }
    
        void movieEvent(Movie movie1) {
        movie1.read();
        }
    
         void draw() {
    
        background(155);
    
        if (mouseX > bx-(boxSize/2) && mouseX < bx+(boxSize/2) &&  
        mouseY > by-(boxSize/2) && mouseY < by+(boxSize/2)) {
        overBox = true; 
    
        } else {
    
        overBox = false;
        }
    
        if ((overBox) && (boxSize < boxMax)) {
    
        image(movie1, bx, by,boxSize++,boxSize++);
        movie1.loop();
    
    
        }
        if ((!overBox)&&(boxSize > boxMin)){
    
        image(movie1, bx, by,boxSize-=3,boxSize-=3);
        movie1.pause();
    
    
        }
        image(movie1,bx,by,boxSize,boxSize);
    
        if (canTrigger && overBox) {
        sample.trigger();
        canTrigger = false;
    
        }
    
        if (!overBox) {
        sample.stop();
        canTrigger = true;              
        }
    
        }
    
  • edited April 2016

    Here is a solution. The problem in line 42/43 in your original code the keyword this refered to a Tile object in you original code (without the class) it was a PApplet object.

    The function call requires a PAppelet object.

    import processing.video.*;
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    
    Minim minim;
    AudioSample sample;
    
    Tile t1;                
    
    void setup() {
      fullScreen(2);
      imageMode(CENTER);
      minim = new Minim(this);
      t1 = new Tile(this, 400, 500, 25, 25);
      //t1.setMovie("TORCH_2_1.mov");
      //t1.setAudio(minim, "Paper Rustle_1.mp3");
    }
    
    
    void draw() {
      background(155);
    }
    
    class Tile {
      PApplet app;
      int cx, cy; // centre of box
      int w, h; // width and height of box
      Movie movie = null;
      AudioSample sample = null;
    
      boolean overBox = false;
      boolean canTrigger = true;
      float boxMax = 100;
      float boxMin = 25;
      int boxSize;
    
      Tile(PApplet papp, int px, int py, int bw, int bh) {
        app = papp;
        cx = px;
        cy = py;
        w = bw;
        h = bh;
        // left this in for you code below
        boxSize = 25;
      }
    
      void setMovie(String fname) {
        movie = new Movie(app, fname);
        movie.loop();
        movie.pause();
      }
    
      void setAudio(Minim m, String fname) {
        sample = m.loadSample(fname);
      }
    
      void isOver(float x, float y) {
        overBox = x > cx - w/2 && x < cx + w/2 && y > cy - h/2 && y < cy + h/2;
      }
    
      void presentTile(float bx, float by) {
        if (movie.available() == true) 
          movie.read(); 
    
        isOver(bx, by);
    
        if (overBox && (boxSize < boxMax)) {
          image(movie, bx, by, boxSize++, boxSize++);
          movie.loop();
        }
        if (!overBox && boxSize > boxMin) {
          image(movie, bx, by, boxSize-=3, boxSize-=3);
          movie.pause();
        }
        image(movie, bx, by, boxSize, boxSize);
    
        if (canTrigger && overBox) {
          sample.trigger();
          canTrigger = false;
        }
    
        if (!overBox) {
          sample.stop();
          canTrigger = true;
        }
      }
    }
    
  • Thanks for the help - really appreciated!

    Unfortunately I can't seem to get that sketch to work. This is as far as I had got;

    import processing.video.*;
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioSample sample;
    
    Tile t1;                
    
    void setup() {
      fullScreen(2);
      imageMode(CENTER);
      minim = new Minim(this);
      t1 = new Tile(this, 400, 500, 25, 25);
      //t1.setMovie("TORCH_2_1.mov");
      //t1.setAudio(minim, "Paper Rustle_1.mp3");
    }
    
    
    void draw() {
      background(155);
    
    
      t1.setMovie("TORCH_2_1.mov");
      t1.setAudio(minim,"Paper Rustle_1.mp3");
      t1.presentTile(200,200);
      t1.isOver(0,0);
    
    
    
    }
    
    class Tile {
      PApplet app;
      int cx, cy; // centre of box
      int w, h; // width and height of box
      Movie movie = null;
      AudioSample sample = null;
    
      boolean overBox = false;
      boolean canTrigger = true;
      float boxMax = 100;
      float boxMin = 25;
      int boxSize;
    
      Tile(PApplet papp, int px, int py, int bw, int bh) {
        app = papp;
        cx = px;
        cy = py;
        w = bw;
        h = bh;
        // left this in for you code below
        boxSize = 25;
      }
    
      void setMovie(String fname) {
        movie = new Movie(app, fname);
        movie.loop();
        movie.pause();
      }
    
      void setAudio(Minim m, String fname) {
        sample = m.loadSample(fname);
      }
    
      void isOver(float x, float y) {
        overBox = x > cx - w/2 && x < cx + w/2 && y > cy - h/2 && y < cy + h/2;
      }
    
      void presentTile(float bx, float by) {
        if (movie.available() == true) 
          movie.read(); 
    
        isOver(bx, by);
    
        if (overBox && (boxSize < boxMax)) {
          image(movie, bx, by, boxSize++, boxSize++);
          movie.loop();
        }
        if (!overBox && boxSize > boxMin) {
          image(movie, bx, by, boxSize-=3, boxSize-=3);
          movie.pause();
        }
        image(movie, bx, by, boxSize, boxSize);
    
        if (canTrigger && overBox) {
          sample.trigger();
          canTrigger = false;
        }
    
        if (!overBox) {
          sample.stop();
          canTrigger = true;
        }
      }
    }
    

    This unfortunately results in;

    ==== JavaSound Minim Error ==== ==== Don't know the ID3 code PRIV

    Also - Im unsure of the functionality of PApplet object and I couldn't find any reference to it so if you could point me in the direction of any more info on it that would be great as it may help me understand why its needed.

    Thanks again for the help!

  • First thing to try is to delete lines 29 & 30, you should not load resources inside the draw method. The draw method is executed 60 times a second.

    Instead uncomment lines 20 & 21. They were commented out because I did not have the movie and sound file.

    When you create a sketch it is of type PApplet and the this keyword refers to the sketch object. When you move the this keyword into the Tile class it refers to the Tile object. I have made a reference to the PApplet object in the Tile class so we can refer to it when needed.

    Report back on what happened.

  • Thanks - I feels like Im getting close now :) This still is not working correctly - but Im understanding the use of PApplet better now and how "this" was in fact looking for the tile class and no longer the sketch - thanks! what I now have is below with the lines moved out of draw like you suggested. I am still getting the same error

    ==== JavaSound Minim Error ==== ==== Don't know the ID3 code PRIV

    And I am a little unsure what arguments I am supposed to be placing in the "t1.isOver" function as well - which may be why the over box detection is not working. The tile is presented though with a paused frame but still does not activate when the mouse is over it.

    import processing.video.*;
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioSample sample;
    
    Tile t1;                
    
    void setup() {
      fullScreen(2);
      imageMode(CENTER);
      minim = new Minim(this);
      t1 = new Tile(this, 400, 500, 25, 25);
      t1.setMovie("TORCH_2_1.mov");
      t1.setAudio(minim, "Paper Rustle_1.mp3");
    }
    
    
    void draw() {
      background(155);
    
    
    
    
      t1.presentTile(200,200);
      t1.isOver(0,0);
    
    
    
    }
    
    class Tile {
      PApplet app;
      int cx, cy; // centre of box
      int w, h; // width and height of box
      Movie movie = null;
      AudioSample sample = null;
    
      boolean overBox = false;
      boolean canTrigger = true;
      float boxMax = 100;
      float boxMin = 25;
      int boxSize;
    
      Tile(PApplet papp, int px, int py, int bw, int bh) {
        app = papp;
        cx = px;
        cy = py;
        w = bw;
        h = bh;
        // left this in for you code below
        boxSize = 25;
      }
    
      void setMovie(String fname) {
        movie = new Movie(app, fname);
        movie.loop();
        movie.pause();
      }
    
      void setAudio(Minim m, String fname) {
        sample = m.loadSample(fname);
      }
    
      void isOver(float x, float y) {
        overBox = x > cx - w/2 && x < cx + w/2 && y > cy - h/2 && y < cy + h/2;
      }
    
      void presentTile(float bx, float by) {
        if (movie.available() == true) 
          movie.read(); 
    
        isOver(bx, by);
    
        if (overBox && (boxSize < boxMax)) {
          image(movie, bx, by, boxSize++, boxSize++);
          movie.loop();
        }
        if (!overBox && boxSize > boxMin) {
          image(movie, bx, by, boxSize-=3, boxSize-=3);
          movie.pause();
        }
        image(movie, bx, by, boxSize, boxSize);
    
        if (canTrigger && overBox) {
          sample.trigger();
          canTrigger = false;
        }
    
        if (!overBox) {
          sample.stop();
          canTrigger = true;
        }
      }
    }
    
  • Answer ✓

    The main problem is in lines 31 and 32 because your sketch is not responding to the mouse position and [200,200] is not over the box. And in line 22 you are asking if [0,0] is over the box which it isn't.

    The code below works fine for me but you will have to alter the code in lines 19 and 20 to use your files.

    The minim message is a warning which you can ignore, I get them all the time. When I ran the sketch below I got

    ==== JavaSound Minim Error ====
    ==== Don't know the ID3 code TXXX

    but it still played the sound file.

    Note that in the draw method I am using the current mouse position rather than fixed coordinates. Also it calls a new method in Tile called display() which makes it easier to see where the box is and thereby test the code.

    I have also update the presentTile method to make it safe to use if you only have the movie or the sound sample.

    import processing.video.*;
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioSample sample;
    Tile t1;                
    
    void setup() {
      fullScreen(2);
      imageMode(CENTER);
      minim = new Minim(this);
      t1 = new Tile(this, 400, 500, 25, 25);
      t1.setMovie("fawlty towers.mp4");
      t1.setAudio(minim, "bewitched.mp3");
    }
    
    void draw() {
      background(155);
      t1.display();
      t1.presentTile(mouseX, mouseY);
    }
    
    class Tile {
      PApplet app;
      int cx, cy; // centre of box
      int w, h; // width and height of box
      Movie movie = null;
      AudioSample sample = null;
    
      boolean overBox = false;
      boolean canTrigger = true;
      float boxMax = 100;
      float boxMin = 25;
      int boxSize;
    
      Tile(PApplet papp, int px, int py, int bw, int bh) {
        app = papp;
        cx = px;
        cy = py;
        w = bw;
        h = bh;
        // left this in for you code below
        boxSize = 25;
      }
    
      void setMovie(String fname) {
        movie = new Movie(app, fname);
        movie.loop();
        movie.pause();
      }
    
      void setAudio(Minim m, String fname) {
        sample = m.loadSample(fname, 512);
      }
    
      void isOver(float x, float y) {
        overBox = x > cx - w/2 && x < cx + w/2 && y > cy - h/2 && y < cy + h/2;
      }
    
      void display() {
        noStroke();
        fill(200, 200, 255);
        rect(cx - w/2, cy - h/2, w, h);
      }
    
      void presentTile(float bx, float by) {
        isOver(bx, by);
        if (movie != null) {
          if (movie.available() == true) 
            movie.read(); 
          if (overBox && (boxSize < boxMax)) {
            image(movie, bx, by, boxSize++, boxSize++);
            movie.loop();
          }
          if (!overBox && boxSize > boxMin) {
            image(movie, bx, by, boxSize-=3, boxSize-=3);
            movie.pause();
          }
          image(movie, bx, by, boxSize, boxSize);
        }
        if (sample != null) {
          if (canTrigger && overBox) {
            sample.trigger();
            canTrigger = false;
          }
          if (!overBox) {
            sample.stop();
            canTrigger = true;
          }
        }
      }
    }
    
  • edited April 2016

    This is brilliant - thanks for the help. I now have a sketch working with multiple tile objects with sound and .movs. I changed your code slightly as I think I had maybe not explained my intentions completely clearly. For posterity I have included it below.

    It seems to all be working correctly now - except that the tiles are not always returning to their original size (perhaps because they are in fact returning to the original size minus 3 pixels? (see line 92 )

    I am also now going to try creating an array of objects so I don't have to setAudio and setMovie each time I add a new tile object - and add a function which only allows one tile to be active at one time. But as you answered my original question perhaps if i have a problem with this I should start a new discussion.

    Once again thanks for the help - very much appreciated.

    import processing.video.*;
    
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    AudioSample sample;
    Tile t1; 
    Tile t2;
    Tile t3;
    Tile t4;
    
    void setup() {
      fullScreen(2);
      imageMode(CENTER);
      minim = new Minim(this);
    
      t1 = new Tile(this);
      t1.setMovie("TORCH_2_1.mov");
      t1.setAudio(minim, "Paper Rustle_1.mp3");
    
      t2 = new Tile(this);
      t2.setMovie("TORCH_2_1.mov");
      t2.setAudio(minim, "Paper Rustle_1.mp3");
    
      t3 = new Tile(this);
      t3.setMovie("TORCH_2_1.mov");
      t3.setAudio(minim, "Paper Rustle_1.mp3");
    
      t4 = new Tile(this);
      t4.setMovie("TORCH_2_1.mov");
      t4.setAudio(minim, "Paper Rustle_1.mp3");
    }
    
    void draw() {
      background(155);
    
      t1.presentTile(200, 200);
      t2.presentTile(225, 200);
      t3.presentTile(250, 200);
      t4.presentTile(275, 200);
    
    
    }
    
    class Tile {
      PApplet app;
      Movie movie = null;
      AudioSample sample = null;
    
      boolean overBox = false;
      boolean canTrigger = true;
      float boxMax = 100;
      float boxMin = 25;
      int boxSize;
    
      Tile(PApplet papp) {
        app = papp;
        boxSize = 25;
      }
    
      void setMovie(String fname) {
        movie = new Movie(app, fname);
        movie.loop();
        movie.pause();
      }
    
      void setAudio(Minim m, String fname) {
        sample = m.loadSample(fname, 512);
      }
    
      void isOver(float x, float y) {
        overBox = (mouseX > x-(boxSize/2) && mouseX < x +(boxSize/2) &&  
        mouseY > y-(boxSize/2) && mouseY < y+(boxSize/2)); 
      }
    
    
      void presentTile(float bx, float by) {
        isOver(bx, by);
        if (movie != null) {
          if (movie.available() == true) 
            movie.read(); 
          if (overBox && (boxSize < boxMax)) {
            image(movie, bx, by, boxSize++, boxSize++);
            movie.loop();
          }
          if (!overBox && boxSize > boxMin) {
            image(movie, bx, by, boxSize-=3, boxSize-=3);
            movie.pause();
          }
          image(movie, bx, by, boxSize, boxSize);
        }
        if (sample != null) {
          if (canTrigger && overBox) {
            sample.trigger();
            canTrigger = false;
          }
          if (!overBox) {
            sample.stop();
            canTrigger = true;
          }
        }
      }
    }
    
Sign In or Register to comment.