# Program for a museum using video and sound library

**URL:** <https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406>\
**Category:** Libraries\
**Tags:** homework, contributions\
**Created:** [July 12, 2023, 9:43pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406 "2023-07-12T21:43:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rociodp](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rociodp](https://discourse.processing.org/u/rociodp)\
**Post date:** [July 12, 2023, 9:43pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/1 "2023-07-12T21:43:19Z")

</div>

Hello forum 🙂

. I have a problem with the program I’m developing. I’m a beginner in processing, and I’m working on an interactive program for a museum exhibition. It contains an array of approximately 20 videos, each around 1 MB in size. To switch between videos, I used a state diagram and mouse events. The problem is that after a certain amount of time, the program consumes a lot of computer resources and stops working. I think it’s because I play the videos in the draw function (within a condition) and they never stop playing.

Additionally, I can’t delete the videos because users can go back interactively, so that video is still requested.

Do you have any suggestions for organizing and optimizing the code to reduce memory consumption?

Thank you!

This is the code, I eliminated some screens so that reading the code is not so heavy, but the logic repeats itself.:

```auto
import processing.sound.*;
import processing.video.*;

Movie protector; 
Movie inicio;

Movie [] naranja;
Movie [] amarillo;
Movie [] azul;
Movie [] verde;
Movie [] rojo;
Movie [] violeta;

PImage inicioFoto;

SoundFile guarani, mapuche, qom;
boolean isPlayingMapuche = false;
boolean isPlayingQom = false;
boolean isPlayingGuarani = false;

SoundFile currentAudio = null;

boolean loopProtector = true;
boolean sePusoFoto = false;
boolean seBorraVideo = false;

int estado = 100;

void setup(){
  
  
protector = new Movie(this, "protector.mp4");
protector.loop();

inicio = new Movie(this, "inicio.mp4");

naranja = new Movie[3];
naranja[0] = new Movie(this, "naranjauno.mp4");
naranja[1] = new Movie(this, "naranjados.mp4");
naranja[2] = new Movie(this, "naranjatres.mp4");

amarillo = new Movie[3];
amarillo[0] = new Movie(this, "amarillouno.mp4");
amarillo[1] = new Movie(this, "amarillodos.mp4");
amarillo[2] = new Movie(this, "amarillotres.mp4");

guarani = new SoundFile(this, "guarani.mp3");
mapuche = new SoundFile(this, "mapuche.mp3");
qom = new SoundFile(this, "qom.mp3");

fullScreen();
}

void draw(){
  
if (loopProtector) {
  if (protector.time() >= protector.duration()) {
      protector.jump(0);
      protector.play();
  }   
}

  //PANTALLA DEL PROTECTOR
if (estado == 100) {
 image(protector, 0, 0, width, height);
 if (mousePressed) {
    loopProtector = false; 
    estado = 0;
    protector.stop();
    protector.dispose();
 }
 
    
 // PANTALLA DE INICIO 
} else if (estado == 0) {
    if (sePusoFoto == true) {
    inicioFoto = loadImage ("inicio.png");
    image(inicioFoto, 0, 0, width, height);
    }
    
     if (sePusoFoto == false){
     image(inicio, 0, 0, width, height);
     inicio.play();
     }

    if (seBorraVideo == true && inicio != null) {
       inicio.stop();
       inicio.dispose();
       inicio = null;
       seBorraVideo = false;
     }
}
   

 // PANTALLAS NARANJAS
else if (estado == 1) {  
image(naranja[0], 0, 0, width, height);
naranja[0].play();

} else if (estado == 2){
image(naranja[1], 0, 0, width, height);
naranja[1].play();

} else if (estado == 3){
image(naranja[2], 0, 0, width, height);
naranja[2].play();
}

/////// PANTALLAS AMARILLAS ////////////////
else if (estado == 4){
image(amarillo[0], 0, 0, width, height);
amarillo[0].play();

} else if (estado == 5){
image(amarillo[1], 0, 0, width, height);
amarillo[1].play();

} else if (estado == 6){
image(amarillo[2], 0, 0, width, height);
amarillo[2].play();
}
}

void mousePressed(){

 ////////////////////////// PANTALLA INICIO ///////////////////////////////////////////////
  if (estado == 0) {
    if (mouseX > 970 && mouseX < 1430 && mouseY > 370 && mouseY < 730) { // Botón naranja 
      estado = 1;
      sePusoFoto = true; 
      seBorraVideo = true;
    } else if (mouseX > 1450 && mouseX < 1930 && mouseY > 740 && mouseY < 1100) { // Botón amarillo
      sePusoFoto = true; 
      seBorraVideo = true;
      estado = 4;
    }
///////////////////////////////////// PANTALLAS NARANJAS /////////////////////////////////////////////////
// ----------- primer pantalla ---------------------------------- 
  
  if (estado == 1) {
    if (mouseX > 270 && mouseX < 420 && mouseY > 910 && mouseY < 980){ //boton inicio
    estado = 0;
  } else if (mouseX > 700 && mouseX < 840 && mouseY > 675 && mouseY < 765){ //boton ley
    estado = 2;
  } else if (mouseX > 890 && mouseX < 1030 && mouseY > 675 && mouseY < 765){ //boton sabias que
    estado = 3;
  }
  }
// ----------- segunda pantalla ----------------------------------   
  if (estado == 2){
    if (mouseX > 75 && mouseX < 215 && mouseY > 885 && mouseY < 955){ //boton volver
    estado = 1;
  } else if (mouseX > 280 && mouseX < 420 && mouseY > 885 && mouseY < 955){ //boton inicio
    estado = 0;
  }
  }
// ----------- tercer pantalla ----------------------------------
 if (estado == 3){
    if (mouseX > 75 && mouseX < 215 && mouseY > 885 && mouseY < 955){ //boton volver
    estado = 1;
  } else if (mouseX > 280 && mouseX < 420 && mouseY > 885 && mouseY < 955){ //boton inicio
    estado = 0;
  } else if (mouseX > 1675 && mouseX < 1725 && mouseY > 810 && mouseY < 860) { // Audio 2
    toggleAudio(mapuche);
  } else if (mouseX > 1675 && mouseX < 1725 && mouseY > 880 && mouseY < 930) { // Audio 3
    toggleAudio(qom);
  } else if (mouseX > 1675 && mouseX < 1725 && mouseY > 950 && mouseY < 1000) { // Audio 4
    toggleAudio(guarani);
  }
  }  
  
 ///////////////////////////////////// PANTALLAS AMARILLAS ///////////////////////////////////////////////// 
// ----------- primer pantalla ----------------------------------   
  if (estado == 4){
    if (mouseX > 270 && mouseX < 420 && mouseY > 910 && mouseY < 980){ //boton inicio
    estado = 0;
  } else if (mouseX > 765 && mouseX < 925 && mouseY > 675 && mouseY < 765){ //boton segunda pantalla amarilla
    estado = 5;
  } else if (mouseX > 1070 && mouseX < 1230 && mouseY > 675 && mouseY < 765){ //boton tercer pantalla amarilla
    estado = 6;
  }  
  }
// ----------- segunda pantalla ---------------------------------- 
  if (estado == 5){
    if (mouseX > 75 && mouseX < 215 && mouseY > 885 && mouseY < 955){ //boton volver
    estado = 4;
  } else if (mouseX > 280 && mouseX < 420 && mouseY > 885 && mouseY < 955){ //boton inicio
    estado = 0;
  }  
  }
// ----------- tercer pantalla ---------------------------------- 
  if (estado == 6){
    if (mouseX > 75 && mouseX < 215 && mouseY > 885 && mouseY < 955){ //boton volver
    estado = 4;
  } else if (mouseX > 280 && mouseX < 420 && mouseY > 885 && mouseY < 955){ //boton inicio
  estado = 0;
  }  
  }
}

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

void toggleAudio(SoundFile audio) {
  if (currentAudio == audio) {
    if (audio.isPlaying()) {
      audio.pause();
      if (audio == mapuche) {
        isPlayingMapuche = true;
      } else if (audio == qom) {
        isPlayingQom = true;
      } else if (audio == guarani) {
        isPlayingGuarani = true;
      }
    } else {
      audio.play();
      if (audio == mapuche) {
        isPlayingMapuche = false;
      } else if (audio == qom) {
        isPlayingQom = false;
      } else if (audio == guarani) {
        isPlayingGuarani = false;
      }
    }
  } else {
    if (currentAudio != null) {
      currentAudio.stop();
    }
    audio.play();
    currentAudio = audio;
    isPlayingMapuche = false;
    isPlayingQom = false;
    isPlayingGuarani = false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 13, 2023, 12:24am UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/2 "2023-07-13T00:24:37Z")

</div>

It would help us to help you if you posted the code which fails.

---

<div class="post-metadata">

**Author:** ![rociodp](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rociodp](https://discourse.processing.org/u/rociodp)\
**Post date:** [July 13, 2023, 1:44am UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/3 "2023-07-13T01:44:04Z")

</div>

ready, it’s a bit long, sorry

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 13, 2023, 2:48am UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/4 "2023-07-13T02:48:51Z")

</div>

Is there supposed to be buttons? How are we to change from one video to the next?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 13, 2023, 10:43am UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/5 "2023-07-13T10:43:55Z")

</div>

Hello @rociodp,

Try VisualVM:

> [@Loading a 100MB PNG and draw it on the canvas but it eats 1GB of RAM frowning](https://discourse.processing.org/t/loading-a-100mb-png-and-draw-it-on-the-canvas-but-it-eats-1gb-of-ram/37791/3):
>
> Hello, There are tools that you can use to monitor memory usage such as VisualVM: [https://visualvm.github.io/](https://visualvm.github.io/) I found this Java.lang.Runtime class useful to monitor memory usage: I created a couple of images in different formats (JPG and PNG) that were 15000x7000 in image size in pixels which is not the same as image size of the file. The image size of the file is in the comments in the code below and here: Results: The size in memory is the same and it make…

Try to determine when the memory increases.  
There is a learning curve to using this but worthwhile.

This is a homework question in a public forum so be mindful of what you are posting and sharing:  
[https://discourse.processing.org/faq#homework](https://discourse.processing.org/faq#homework)

Try to work out the memory problems separately from you code with an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example):

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

Your posted code had a missing bracket.

`:)`

---

<div class="post-metadata">

**Author:** ![rociodp](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rociodp](https://discourse.processing.org/u/rociodp)\
**Post date:** [July 13, 2023, 6:05pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/6 "2023-07-13T18:05:22Z")

</div>

Yes, the buttons work using mouse functions (mousePressed).

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 13, 2023, 6:30pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/7 "2023-07-13T18:30:06Z")

</div>

I’m sorry, but I don’t see any buttons on my system:

 ![scrn](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/9/09d6e0dc3e22cde4bf8e8d9a10ce1ecda1379d43.png)

---

<div class="post-metadata">

**Author:** ![rociodp](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rociodp](https://discourse.processing.org/u/rociodp)\
**Post date:** [July 13, 2023, 9:34pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/8 "2023-07-13T21:34:58Z")

</div>

what happens is that the buttons are visible in the video files, and I delimit their coordinates with mouse pressed, so that’s why you wont see them.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [July 14, 2023, 1:54am UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/9 "2023-07-14T01:54:53Z")

</div>

I’m unable to reproduce the problem and therefore am unable to contribute a solution. I did notice that there are a lot of xxx.play() calls in draw(), but I don’t see that the videos are ever stopped (ie xxx.stop()). You used xxx.dispose() a couple of times but I can’t find that in the documentation and don’t know if it actually works or not.

---

<div class="post-metadata">

**Author:** ![rociodp](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@rociodp](https://discourse.processing.org/u/rociodp)\
**Post date:** [July 18, 2023, 10:20pm UTC](https://discourse.processing.org/t/program-for-a-museum-using-video-and-sound-library/42406/10 "2023-07-18T22:20:56Z")

</div>

okay, i’ll keep working and searching, thank u so much for your response
