Load a Movie only if I need it

edited January 2015 in How To...

In the example on how to play a movie,

import processing.video.*;

Movie theMov;

void setup() { size(500, 500); theMov = new Movie(this, "peanut.mov"); theMov.loop(); }

void draw() { image(theMov, 0, 0); }

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


In this example, the line

theMov = new Movie(this, "peanut.mov");

is in setup

For my project, I want to create lists of 100s of "words", with a short movie for each word in the data folder. The user makes some choices, and based on that, a random word ("peanut") is pulled out from the list and the movie with that name is displayed ("peanut.mov");

Is the only way to display that one movie to create Movie objects in setup for all of the movies at the start in setup()?

(when I move the "new Movie" line to draw(), the image is stuck on the first frame, I imagine because the movie gets reloaded for each frame).

If so I am going to need a different Movie object name for each (word,movie) pair. I would need to read all the words in the file containing the list and in the loop I need some code that would look like

(assuming String myWord = "peanut";)

Eval_this_and_Make_this_into_a_variable_name("TheMov" + myWord) = new Movie(this, myWord + ".mov");

Since I don't believe Java can create a variable name at runtime, is there another way of doing what I want to do: to have the list of words and the movies names separate from the program so I can change my list of words, or modify the list etc without having to modify the program.

Tagged:

Answers

  • Answer ✓

    > (when I move the "new Movie" line to draw(), the image is stuck on the first frame, I imagine because the movie gets reloaded for each frame). see code below

    as for the other question, if you dont want to hardcode your arrays you can create 2 (or 1) .txt with the ordered list of your words + delimiter, and videos, then load them in setup(), split them and populate the 2 arrays in a loop. You can also create 1 2D array. Of course the number of words must be = to the number of videos

    import processing.video.*;
    
    Movie theMov; 
    boolean isPlaying = false;
    
    
    boolean wordChoosen = false;
    String[] myWords= {"word1"};
    String[] myVideos= {"ohms.mp4"};
    int whichOne;
    
    void setup() { 
      size(800, 600,P2D);
    
    }
    
    
    void draw() { 
     // background(0);
      if(wordChoosen && isPlaying == false){
    theMov = new Movie(this, myVideos[whichOne]);
         theMov.loop();
    
      isPlaying = true;
    
    
      }
    
      if(isPlaying){
      image(theMov, 0,0, 800,600);
     // theMov.noLoop();//as you wan
      }
    } ;
    
    void movieEvent(Movie m) {
      if (wordChoosen && isPlaying) {
        m.read();
      }
    
    };
    
    void mouseReleased(){
      if(!wordChoosen){
     whichOne= int(random(myWords.length));
        wordChoosen = true;
        isPlaying = false;
      }else{
        wordChoosen = false;
        isPlaying = false;
       theMov.stop();
       theMov.dispose();
      background(0);
      }
    };
    
  • I did not see what Gotoloop wanted me to look at in the links he suggested.

    I think the next idea I tried was similar to that of Akenaton. I moved setup to a separate file on a separate tab, and I had a movie variable for each movie, and a hash to link words in the list and the movies.

    I figured I could write a program that would generate the exact setup file needed for a particular word list.

    But I have thousands of words and I don't want to mix my data and my code. I want the code to work with any list of words as long as the videos are in the data folder and having to generate a set up file each time is prone to errors.

    What I ended up doing is giving up on the user making a choice after the program had started. So now the user choses the parameters in the code in setup (I did not see a way to pass parameter to setup any other way). That allowed me to use those parameter to select the word I will display in setup and load that movie in a generic movie variable name. And then the program stops. One downside is that I have to restart the game for each new word, but that is not too bad.

    I decided the separation of data and program and the ease of use was more important then having a nice user interface that would allow me to share the program with others and have others use it.

  • Actually, I think I jumped too fast to the wrong conclusion. I think Akenaton's code could work with my original setup since he is using only one Movie variable. Very cool. Thank you. I will try it.

Sign In or Register to comment.