[SOLVED] Not smooth playback and occasional sketch freeze with GSVideo
in
Contributed Library Questions
•
1 year ago
EDIT: set framerate to be the same as the framereate of the movie and playback is much smoother. No freezes anymore :0
My sketch picks a random video from a set of about 60 movies. All movies are 1024 x 768 px, .avi format. Movies take about 3 seconds
I declare a movie by
- GSMovie movie
Then the movie object is instantiated in the function setNextMovie():
- movie = new GSMovie(this, "test_movie.avi");
I start the movie by calling:
- movie.play()
then i stop the movie by calling:
- movie.stop()
Then i call setNextMovie again which assigns "movie" to a new movie.
The filename is kept test_movie for, well, testing purposes.
. When i run the sketch everything is fine. After playing a few movies (i.e. 15 to 20) the sketch becomes unresponsive.
I still get respons from my controlP5 buttons, but the video remains frozen, and choosing setnextMovie() and START movie does not yield any effect.
Is this a common issue? What is the best video format for GSvideo?
I could make the movies smaller, but i want to be sure that it would solve the issue.
I post my code hereunder, its large, i know but it is working. Please download the sample movie from
here, and put it in your data folder inside the sketch folder.
Running the sketch, and choosing START movie, STOP movie, SETUP NEXT movie should freeze the sketch after a while.
I guess my working memory is filling up after some movies, i might have to destroy the movie objects?
Thanks alot in advance!
- /**
- This program receives motion data from phone
- */
- import java.io.*;
- import codeanticode.gsvideo.*;
- import controlP5.*;
- import oscP5.*;
- import netP5.*;
- NetAddress myRemoteLocation;
- OscP5 oscP5;
- ControlP5 controlP5;
- ControlWindow controlWindow;
- Textfield p_field;
- Textlabel p_label;
- //so we can write
- PFont myFont;
- //PrintWriter output;
- FileWriter output;
- GSMovie movie;
- //to store participant ID
- String participant;
- //these hold the motion values
- float x, y, z;
- //needed for motion recording
- boolean record;
- int cntr = 0;
- //data sequences are stored as vectors
- PVector [] mdata = new PVector [2000];
- //the number of movies in each pos
- int n_movies = 19;
- //string holding the movie name
- String movie_filename;
- //to count which movie is next
- int m_counter = 0;
- int p_counter = 0;
- //needed for order of the performed gestures
- int [] p_1 = new int [n_movies];
- int [] p_2 = new int [n_movies];
- int [] p_3 = new int [n_movies];
- //need for random order of positions
- int [] position_name = new int [3];
- //to keep the order of the positions
- int [] p_order = new int [3];
- //to store which movies have been shown
- int [] movie_name = new int [n_movies];
- /////////////////////////////////////////////////////SETUP/////////////////////////////////////////////
- void setup() {
- //setup the canvas
- size(1024, 720, P3D);
- frameRate(100);
- background(0);
- myFont = createFont("FFScala", 32);
- textFont(myFont);
- //make a connection with the OpenSoundControlProtocol
- oscP5 = new OscP5(this, 25002);
- myRemoteLocation = new NetAddress("192.168.1.82", 25002);
- //create an interface framework
- controlP5 = new ControlP5(this);
- controlP5.setAutoDraw(true);
- //controlP5.setControlFont(new ControlFont(createFont("Verdana", 15)));
- //create secondary control window for controlling video playback and motion recording
- controlWindow = controlP5.addControlWindow("ControlWindow", 100, 100, 400, 400);
- controlWindow.setTitle("Controls");
- //create control objects
- p_field = controlP5.addTextfield("ParticipantID", 50, 5, 150, 25);
- Button startButton = controlP5.addButton("Start Record", 1, 50, 50, 150, 50);
- Button stopButton = controlP5.addButton("Stop Record", 2, 50, 110, 150, 50);
- Button storeButton = controlP5.addButton("Store Record", 3, 50, 170, 150, 50);
- Button movieStart = controlP5.addButton("Start Movie", 4, 220, 50, 150, 50);
- Button moviePause = controlP5.addButton("Stop Movie", 5, 220, 110, 150, 50);
- Button nextMovie = controlP5.addButton("Setup Next Movie", 6, 220, 170, 150, 50);
- Button deleteRecord = controlP5.addButton("Clear Gesture Data", 7, 50, 290, 150, 50);
- //Toggle recordStartStop = controlP5.addToggle("Toggle to start and stop", false, 50, 550, 550, 125);
- //p_label = controlP5.addTextlabel("ParticipantLabel", "NOT SET", 600, 5);
- //position buttons on the controlwindow
- p_field.setWindow(controlWindow);
- startButton.setWindow(controlWindow);
- stopButton.setWindow(controlWindow);
- storeButton.setWindow(controlWindow);
- movieStart.setWindow(controlWindow);
- moviePause.setWindow(controlWindow);
- nextMovie.setWindow(controlWindow);
- deleteRecord.setWindow(controlWindow);
- //recordStartStop.setWindow(controlWindow);
- //p_label.setWindow(controlWindow);
- //populate mdata with actual Vector objects
- for (int i = 0; i < mdata.length; i++) {
- mdata[i] = new PVector();
- }
- //populate movies with actual filenames
- for (int i = 0; i < movie_name.length; i++) {
- movie_name [i] = i + 1;
- }
- //populate positions with actual positions
- for (int i = 0; i < 3; i++) {
- position_name [i] = i + 1;
- }
- //create an array with the order of the positions
- for (int i = 0; i < 3; i++) {
- p_order [i] = getUniquePosition();
- }
- //get an array with order of to be performed gestures for each position
- //p_1 is sit, p_2 is stand, p_3 is walk
- for (int i = 0; i < n_movies; i++ ) {
- p_1 [i] = getUniqueMovie();
- //if last position is reached, we should repopulate the movie_name array
- if (i == n_movies - 1) {
- for (int j = 0; j < movie_name.length; j++) {
- movie_name [j] = j + 1;
- }
- }
- }
- for (int i = 0; i < n_movies; i++ ) {
- p_2 [i] = getUniqueMovie();
- //if last position is reached, we should repopulate the movie_name array
- if (i == n_movies - 1) {
- for (int j = 0; j < movie_name.length; j++) {
- movie_name [j] = j + 1;
- }
- }
- }
- for (int i = 0; i < n_movies; i++ ) {
- p_3 [i] = getUniqueMovie();
- //if last position is reached, we should repopulate the movie_name array
- if (i == n_movies - 1) {
- for (int j = 0; j < movie_name.length; j++) {
- movie_name [j] = j + 1;
- }
- }
- }
- //setup first movies
- setNextMovie();
- }
- ////////////////////////////////////////////DRAW////////////////////////////////////////////////
- void draw() {
- //draw the movies on the screen
- image(movie, 0, 0);
- //image(profile_view, 640, 0);
- controlP5.draw();
- }
- ///////////////////////////////////////////Button Event Handling/////////////////////////////////
- public void controlEvent(ControlEvent theEvent) {
- int button = int(theEvent.controller().defaultValue());
- switch (button) {
- case 0:
- //set the label of participant id
- participant = theEvent.controller().stringValue();
- println(participant);
- break;
- case 1:
- startRecord();
- break;
- case 2:
- stopRecord();
- break;
- case 3:
- storeRecord();
- break;
- case 4:
- startMovie();
- break;
- case 5:
- stopMovie();
- break;
- case 6:
- setNextMovie();
- break;
- case 7:
- dumpRecord();
- break;
- }
- }
- void keyPressed () {
- if (key == ' ') {
- if (record == true) {
- stopRecord();
- println("STOP record");
- }
- else {
- startRecord();
- println("START record");
- }
- }
- if (key == 's') {
- println(key);
- storeRecord();
- }
- }
- void toggle(boolean theFlag) {
- if (theFlag==true) {
- record = true;
- println("recording");
- }
- }
- ///////////////////////////////////////////Motion Recording//////////////////////////////////////
- public void startRecord() {
- cntr = 100;
- record = true;
- }
- public void stopRecord() {
- record = false;
- }
- public void storeRecord() {
- //write participant id as comment
- try {
- output.write( "/* " + participant + " */");
- }
- catch (IOException e)
- {
- println ("error in participant write: " + e);
- }
- //once records stops we write the string to the output file
- for (int i = 0; i < cntr; i++) {
- String out = "[" + mdata[i].x + "\t" + mdata[i].y + "\t" + mdata[i].z + "]; ";
- try {
- output.write(out);
- }
- catch (IOException e)
- {
- println ("error in stp rec: " + e);
- }
- }
- try {
- output.write("\n");
- output.flush(); // Write the remaining data
- output.close(); // Finish the file
- println("GESTURE SEQUENCE STORED");
- }
- catch (IOException e)
- {
- println ("error in stp rec write: " + e);
- }
- }
- public void dumpRecord() {
- for (int i = 0 ; i < cntr + 1; i++) {
- mdata[i].x = 0;
- mdata[i].y = 0;
- mdata[i].z = 0;
- }
- println("LAST RECORD DELETED");
- }
- //////////////////////////////////////////Movie Control/////////////////////////////////////////
- //setup the next movie
- public void setNextMovie() {
- //println("pcounter: " + p_counter);
- int m = p_order[p_counter];
- switch (m) {
- case 1:
- movie_filename = "s " + p_1 [m_counter] + ".avi";
- break;
- case 2:
- movie_filename = "st " + p_2 [m_counter] + ".avi";
- break;
- case 3:
- movie_filename = "w " + p_3 [m_counter] + ".avi";
- break;
- }
- if (m_counter == n_movies - 1) {
- //if we reached the last movie in this position category
- //go to next category
- p_counter++;
- m_counter = -1;
- }
- println("mcounter: " + m_counter);
- m_counter++;
- // init movies, download the movie form
- // http://dl.dropbox.com/u/22416352/test_movie.avi
- movie = new GSMovie(this, "test_movie.avi");
- println("NEXT MOVIE IS: " + movie_filename);
- setNextGestureRecord();
- }
- //start the movie
- public void startMovie() {
- println("MOVIE START");
- //TODO get movie to play 2 times
- movie.loop();
- }
- //pauze or stop playback
- public void stopMovie() {
- println("MOVIE STOP");
- movie.stop();
- }
- /////////////////////////////////////////////////////////////////Gesture Record Control////////////////////////////////////
- public void setNextGestureRecord() {
- //get a file writer to store the data in a file corresponding to the performed gesture
- String [] parts = split (movie.getFilename(), '.');
- String filename = parts[0];
- try {
- output = new FileWriter("D:\\" + "sequences\\" + filename + ".seq", true);
- }
- catch(IOException ioe) {
- println("error in setup: " + ioe);
- }
- }
- /////////////////////////////////////////////////////////////////////////Utils////////////////////////////////////////////
- //gets a unique movie name
- public int getUniqueMovie() {
- int pos = int(random(0, n_movies));
- //pick movie based on position
- int unique = movie_name[pos];
- //make the value 0 so that we can identify that the movie has been picked
- movie_name[pos] = 0;
- //when movie has been picked its value has been set to zero, so we pick again
- if (unique == 0) {
- unique = getUniqueMovie();
- }
- return unique;
- }
- public int getUniquePosition() {
- int pos = int(random(0, 3));
- //pick movie based on position
- int unique = position_name[pos];
- //make the value 0 so that we can identify that the movie has been picked
- position_name[pos] = 0;
- //when movie has been picked its value has been set to zero, so we pick again
- if (unique == 0) {
- unique = getUniquePosition();
- }
- return unique;
- }
- //this reads the image from the movie in order to display it
- void movieEvent(GSMovie thisMovie) {
- thisMovie.read();
- }
- //Function reads motion data
- void oscEvent(OscMessage theOscMessage) {
- if (theOscMessage.checkAddrPattern("/motion"))
- {
- //print("getting it: ");
- if (record == true) {
- mdata[cntr].x = theOscMessage.get(0).floatValue();
- mdata[cntr].y = theOscMessage.get(1).floatValue();
- mdata[cntr].z = theOscMessage.get(2).floatValue();
- if (frameCount % 10 == 1) {
- println(cntr + ": "+ mdata[cntr].x);
- }
- cntr++;
- }
- }
- }
1