How to swap and play video files using functions

edited April 16 in Library Questions

Hi guys, I'm having some trouble getting this interactive piece to work. I want to hover over the top left of my sketch and have Scene 1 play. Then a minute later, hover over it and have Scene 2 play and around a minute and a half later, have Scene 3 play. However, the mouseX and mouse Y commands are ignored after Scene 1 plays through and when I input a timer, it is also ignored. In my final code, each scene function will contain 7 videos, so that's why I'm using "myMovie" as my base code and swapping different movie files in. For example, I will have myMovie1, myMovie2, myMovie3, myMovie4 etc, and load Scene 1's 7 files, dispose of them, then move onto scene 2's 7 files and eventually Scene 3's.

If anyone has any advice or can test this out it would be greatly appreciated. Thanks everyone!

import processing.video.*; 

Movie myMovie;

boolean mouseInCorrectLocationToPlayVideo;

boolean hasMovieEnded;

float movieEndDuration = 0.1;

int whichSceneIAmCurrentlyOn = 0; 

void setup(){
size(640, 480);
frameRate(30);

loadScene1();

}    

void draw(){
println(millis());

    //Checking if movie has ended.
    //If movie has not yet ended, check mouseX & Y location. 
    // If mouse is in these boundaries, mouseInCorrectLocationToPlayVideo is true.  
   if (hasMovieEnded == false){
      if ((mouseX < 213) && (mouseY <= 240)){
        if((mouseX > 0) && (mouseY >= 0)){

            mouseInCorrectLocationToPlayVideo = true;  
         }
       }
     }

    //If mouse X & Y are not in these boundaries, print out.   
     if(mouseInCorrectLocationToPlayVideo == false){
     println("not in boundaries");
   }

   //If mouse X & Y are in these boundaries, play video.
   if (mouseInCorrectLocationToPlayVideo == true) { 
    myMovie.play();
    image(myMovie, 0, 0);
    //println("1:" + frameCount);

    //When movie ends, dispose of movie file from memory. 
    if  ((myMovie.time() + movieEndDuration) >= myMovie.duration()){

      myMovie.dispose();
      unregisterMethod("dispose", myMovie);
      g.removeCache(myMovie);
      println("current movie finished file dumped");

      //Movie has ended. 
      hasMovieEnded = true; 
      //MouseX and Y are not within stated boundaries anymore. 
      mouseInCorrectLocationToPlayVideo = false; 

      //  if (millis() > 10*1000){ //not working
        if (whichSceneIAmCurrentlyOn == 1){
           reset();  
           loadScene2();
         }
      //   }
        else if (whichSceneIAmCurrentlyOn == 2){
          reset();   
          loadScene3();
        }  

    }          
  }  
} //closes draw

void reset(){
 // mouseInCorrectLocationToPlayVideo = false; //not needed
  hasMovieEnded = false;
}

void loadScene1(){

   myMovie = new Movie(this, "tileScene1.mp4");
   whichSceneIAmCurrentlyOn = whichSceneIAmCurrentlyOn + 1;
}

void loadScene2(){

    myMovie = new Movie(this, "tileScene2.mp4");
    whichSceneIAmCurrentlyOn = whichSceneIAmCurrentlyOn + 1;
}

void loadScene3(){

    myMovie = new Movie(this, "tileScene3.mp4");
    whichSceneIAmCurrentlyOn = whichSceneIAmCurrentlyOn + 1;
}


void movieEvent(Movie m) {
   if (m == myMovie) {
    myMovie.read();
  }
}

Answers

  • To reach line 50, it has to go through two conditionals in line 42 and 48. This is not good. I mean, you want your program to only execute and go through your movies only when the mouse is on your selected spot in the screen? What should happen if the mouse is not in the right spot?

    Also you are controlling line 58 while my understanding is that it should be controlled within your draw() function based on the mouse position. Nothing wrong to update the value, but I question its functionality.

    I have to add, I don't think you need line 50 and the ones that follow them. As far as i know, there is not need to dispose it. i might be wrong and maybe the practice should be done.

    A suggestion is to have an ListArray of movies which you load when a scene is selected. Then you play each file in the ListArray, as one ends, the next begins. When you reach the end of the list, then you move to the next scene where you load your next set of movies into the array.

    Kf

Sign In or Register to comment.