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.:
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;
}
}