Video halts shortly after loading

Hi

I have this code below that keeps playing random short videos from a folder. They are all around 10 secs. I noticed that randomly once in a while when loading a new video the program halts but gives no error at all. It just stays still in the previous video. We checked that the videos are ok and the error seems pretty random not related to any particular video, as far as we can see.

I noticed that the duration of the video reports a wrong number (far too high). Also that in my system (Ubuntu 20.04) the sound system pulseaudio is having some kind of trouble because the browser wont play any video, VLC plays videos without sound and top says the pulseaudio process is using 30% of the CPU.

Previously I had another version where I would instantiate the new video when the previous video ended but we had the same problem. I thought that instantiating all videos during setup would help but it does not. any ideas? thanks

enrike

import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import processing.video.*;

/////////////////////////////////////////////////////
String VIDEOS = "/media/r2d2/data/videos_akane/cuts2"; //enrike laptop
/////////////////////////////////////////////////////

File file; 
File[] shuffledFiles;
Movie[] movies;
int next = -1;

void setup() {
  frameRate(24);
  //size(1280, 720);
  fullScreen();
  noCursor();
  file = new File(VIDEOS); // loads the videos
  shufflefiles(); //random order the videos into shuffledFiles
  
  movies = new Movie[shuffledFiles.length];
   
  for (int i = 0; i < shuffledFiles.length; i = i+1) {
    String path = shuffledFiles[i].getPath();
    println(i, path);
    movies[i] = new Movie(this, path); // 
  }
  
  runmovie(); // start playing
}

void shufflefiles() {
  shuffledFiles = file.listFiles(); 
  List<File> shuffled = Arrays.asList(shuffledFiles); // get them
  Collections.shuffle(shuffled);
  shuffled.toArray(shuffledFiles); // back in
  println("SHUFFLE FILES BEFORE STARTING");
  printArray(shuffledFiles);
}

void shufflemovies() {
    println("SHUFFLE VIDEOS TO RESTART");
  List<Movie> shuffled = Arrays.asList(movies); // get them
  Collections.shuffle(shuffled);
  shuffled.toArray(movies); // back in
  printArray(shuffledFiles);
}

void runmovie(){
  float speed = 1;//0.1; //random(0.03, 1);
  //println(speed);
  /*if (next>=0){
    println("stop", next);
    movies[next].stop(); // JUST IN CASE stop previous
  }*/
  
  next = next + 1;
  
  if (next>=shuffledFiles.length){ // loop the list again after shuffle
    next=0;
    shufflemovies(); // should shuffle the videos array
  }

  println(next, shuffledFiles[next].getPath());
  movies[next].play();
  movies[next].volume(0); //VOLUME MUTED
  movies[next].speed(speed);
}

void draw() {
  try {
    image(movies[next], 0, 0, displayWidth, displayHeight);
  } catch(Exception e) {
    println("error reading" + str(next));
    runmovie();
  }
  
  if (movies[next].time() >= (movies[next].duration()-0.1)) { // ended
    runmovie();
  } else {
    //println(movies[next].duration()+ "/" + movies[next].time());
  }
}

void movieEvent(Movie m) {
   if (movies[next].available()) {
      m.read();
      println("reading "+ movies[next].time() );
   } else {
     println("not available");
   }
}