I am developing a program which allows me to cross-fade between several movies. For that I built a vector where YYY is a class which inside holds an ofMoviePlayer. In my main loop I then call YYY::update() and YYY::draw() for each vector element.
When I only load one movie everything seems to work fine. But when I load two or more movies it crashes even before getting to draw. Actually it crashes when I try to, for example, do ofMoviePlayer::setPosition(0).
This may be my fault, of course, but I still didn’t figure it out. Could this be a bug with Quicktime engine? Are there any known bugs?
here you are. you just have to make a simple change to the moviePlayerExample:
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofAddons.h"
#include <vector>
class testApp : public ofSimpleApp{
public:
testApp();
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();
vector<ofVideoPlayer> movies; // <<<<< new variable
ofVideoPlayer fingerMovie;
};
#endif
testApp::Setup() in testApp.cpp:
void testApp::setup(){
ofBackground(255,255,255);
fingerMovie.loadMovie("movies/fingers.mov");
fingerMovie.play();
movies.push_back(fingerMovie); // <<< remove this line and it's ok
ofVideoPlayer *movie;
movie = new ofVideoPlayer();
movie->loadMovie("movies/fingers.mov");
movies.push_back(*movie);
movie = new ofVideoPlayer();
movie->loadMovie("movies/fingers.mov");
movies.push_back(*movie);
}
So:
if you don’t add the fingerMovie to the vector everything works fine.
if you add fingerMovie but comment out the 2nd and 3rd movies everything also works fine.
Please note that I didn’t change the rest of the code. I simple add the movie together with two other movies to a vector which is not even used.
I hope this helps.
Please let me know if this is indeed a bug or if I’m doing something wrong.
ok – I know the problem… when you use push_back and pass in the object, it’s doing pass by copy. When it does that, it’s doing some stuff that doesn’t do a proper clone of the object… some info is here:
To be honest, I’m a little afraid of getting tangled in c++ programming. I was using vvvv which is quite fast and easy to code. But since I’m switching to mac I decided to give OF a try. I am quite impressed. And since I’d rather code than patch, I’d really like to adopt it.
But this kind of errors gets me scared because I don’t want to get stuck in this kind of low level stuff which c++ is so full of. I was about to give up on using OF for this project, but if I keep getting such a good support from the community I’m willing to keep on trying
Or you could just do this with the same code you had in your header file
This should work well if you don’t need to be dynamically adding and removing movies from your vector - otherwise arturo’s example should work well for that.
void testApp::setup(){
ofBackground(255,255,255);
//we are going to load and play 3 movies
//assign space for the 3 ofVideoPlayer objects
movies.assign(3, ofVideoPlayer);
//now just load the movies
movies[0].loadMovie("movies/example1.mov");
movies[0].play();
movies[1].loadMovie("movies/example2.mov");
movies[1].play();
movies[2].loadMovie("movies/example3.mov");
movies[2].play();
}
Understood. I think I’ll use a vector of pointers since it completely bypasses the problem. But your example made me better understand what the problem is.
But this kind of errors gets me scared because I don’t want to get stuck in this kind of low level stuff which c++ is so full of. I was about to give up on using OF for this project, but if I keep getting such a good support from the community I’m willing to keep on trying
yep totally – it’s not that easy to solve errors, especially language issues, but for us this kind of posting is good because:
a) we learn about usage, and that shows us flaws in the code (like ofVideoPlayer can’t be used in a vector). there is much we are not able to predict, so feedback is very helpful.
b) it is googleable and linkable – and therefore good for folks in the future
c) alot of c++ stuff is hard to know, and everyone does stuff differently, so it’s good to jot down different ideas and solve problems with others.