Asli Caglar

openFrameworks/ playing video (instructables)

#include “testApp.h”

//————————————————————–

void testApp::setup()

{

serial.setup(“/dev/tty.usbserial-FTFMV6XM”,9600); //to define the serial connection

ofBackground(255,255,255); //to define the background (white)

currentVideo = 0; //0. //to play the video when the program starts working

for(int i = 0; iloadMovie(“movies/oto.mp4″);

movies[0]->stop();

movies[1]->loadMovie(“movies/gida.mp4″);

movies[1]->stop();

movies[2]->loadMovie(“movies/seks.mp4″);

movies[2]->stop();

movies[3]->loadMovie(“movies/aile.mp4″);

movies[3]->stop();

movies[4]->loadMovie(“movies/cocuk.mp4″);

movies[4]->stop();

movies[5]->loadMovie(“movies/kirmizi.mp4″);

movies[5]->stop();

movies[6]->loadMovie(“movies/animasyon.mp4″);

movies[6]->stop();

movies[currentVideo]->play();

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10; //to generate a random number that is a multiple of 10

movies[currentVideo]->setPosition(randomSecond*movies[currentVideo]->getDuration()); //to go to the random second that is generated randomly

volume = 100;

}

//————————————————————–

void testApp::update()

{

movies[currentVideo]->idleMovie();

data = serial.readByte(); //to read the data sent from the remote control

if(data != -2) //-2 //if not

{

if(data != 10 && data != 13) //if the information sent is not a new line

command = command+char(data); //add the character

}

else if(command != “”)

{

int commandInt = atoi(command.c_str()); //convert to number – ascii to integer

ofLogWarning() <= 1 && commandInt stop();

currentVideo = commandInt-1;

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond*movies[currentVideo]->getDuration());

movies[currentVideo]->play();

}

else if(commandInt == 16) //decrease volume

{

if(volume > 0)

{

volume -= 5;

}

movies[currentVideo]->setVolume(volume);

}

else if(commandInt == 17) //increase volume

{

if(volume setVolume(volume);

}

else if(commandInt == 12)

{

}

command = “”;

}

}

//————————————————————–

void testApp::draw()

{

ofSetHexColor(0xFFFFFF);

movies[currentVideo]->draw(0,0,1024,768);

if(int(movies[currentVideo]->getPosition()*movies[currentVideo]->getDuration()*100)%1000 > 990)// if it is 9, 19, 29… point a number go to the other random number that is a multiple of 10

{

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond/movies[currentVideo]->getDuration());

}

if(movies[currentVideo]->getIsMovieDone())

{

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond/movies[currentVideo]->getDuration());

}

}

openFrame Code/ Instructable

#include “testApp.h”

 

//————————————————————–

void testApp::setup()

{

serial.setup(“/dev/tty.usbserial-FTFMV6XM”,9600); //to define the serial connection

 

ofBackground(255,255,255); //to define the background (white)

currentVideo = 0; //0. //to play the video when the program starts working

    

for(int i = 0; i<7; i++)

{

movies[i] = new ofVideoPlayer(); // to define the number of movies (7)

}

movies[0]->loadMovie(“movies/oto.mp4”);

movies[0]->stop();

 

movies[1]->loadMovie(“movies/gida.mp4”);

movies[1]->stop();

 

movies[2]->loadMovie(“movies/seks.mp4”);

movies[2]->stop();

 

movies[3]->loadMovie(“movies/aile.mp4”);

movies[3]->stop();

 

movies[4]->loadMovie(“movies/cocuk.mp4”);

movies[4]->stop();

 

movies[5]->loadMovie(“movies/kirmizi.mp4”);

movies[5]->stop();

 

movies[6]->loadMovie(“movies/animasyon.mp4”);

movies[6]->stop();

 

movies[currentVideo]->play();

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10; //to generate a random number that is a multiple of 10

movies[currentVideo]->setPosition(randomSecond*movies[currentVideo]->getDuration());  //to go to the random second that is generated randomly

volume = 100;

}

 

//————————————————————–

void testApp::update()

{

movies[currentVideo]->idleMovie();

data = serial.readByte(); //to read the data sent from the remote control

 

if(data != -2) //-2 //if not

{

if(data != 10 && data != 13) //if the information sent is not a new line

command = command+char(data); //add the character

}

else if(command != “”)

{

int commandInt = atoi(command.c_str()); //convert to number – ascii to integer

ofLogWarning() << commandInt;

if(commandInt >= 1 && commandInt <= 7)

{

movies[currentVideo]->stop();

currentVideo = commandInt-1;

 

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond*movies[currentVideo]->getDuration());

movies[currentVideo]->play();

 

}

else if(commandInt == 16) //decrease volume

{

if(volume > 0)

{

volume -= 5;

}

 

movies[currentVideo]->setVolume(volume);

 

}

else if(commandInt == 17) //increase volume

{

if(volume < 100)

{

volume += 5;

}

 

movies[currentVideo]->setVolume(volume);

}

else if(commandInt == 12)

{

 

 

}

 

command = “”;

}

 

}

 

//————————————————————–

void testApp::draw()

{

ofSetHexColor(0xFFFFFF);

movies[currentVideo]->draw(0,0,1024,768);

 

if(int(movies[currentVideo]->getPosition()*movies[currentVideo]->getDuration()*100)%1000 > 990)// if it is 9, 19, 29… point a number go to the other random number that is a multiple of 10

{

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond/movies[currentVideo]->getDuration());

}

 

if(movies[currentVideo]->getIsMovieDone())

{

randomSecond = int(ofRandom(0,int(movies[currentVideo]->getDuration()/10)))*10;

movies[currentVideo]->setPosition(randomSecond/movies[currentVideo]->getDuration());

}

}

processing.

import processing.video.*;

void draw()
{
background(0);

image(videos[current], 0, 0, width- 1, height-1);

if(videos[current].time()%10 > 9.5)
{
int newAd = int(random(0, lengths[current]-9));
newAd -= newAd%10; //% – modulus
videos[current].jump(newAd);
}

//println(videos[current]);
}

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

void remoteInput()
{
if(inByte >= ‘0’ && inByte <= ‘9’)
{
command = command + inByte;
}
else if(inByte == 10)
{
println(command);
int commandInt = Integer.parseInt(command);
if (commandInt >= 1 && commandInt <= 7)
{
videos[current].stop();
videos[current].jump(0);

current = commandInt-1;

int newAd = int(random(0, lengths[current]-9));
newAd -= newAd%10; //% – modulus
videos[current].jump(newAd);
videos[current].play();
}
else if (command.equals(“12”))
{
exit();
}
//else if( … )
// {
//  …
//}

command = “”;
}
}

void serialEvent(Serial myPort)
{
if(myPort.available() > 0)
{
inByte = myPort.readChar();
}

remoteInput();
}

import processing.serial.*;
import processing.video.*;

Serial myPort;  // The serial port:
int current = 0;

float f;
float movSpeed;
String command;
char inByte;

Movie[] videos;
int[] lengths;

void setup()
{
size(720, 576);
background(0);

videos = new Movie[7];
lengths = new int[7];

videos[0] = new Movie(this, “gida.mov”);
videos[1] = new Movie(this, “oto.mov”);
videos[2] = new Movie(this, “cocuk.mov”);
videos[3] = new Movie(this, “aile.mov”);
videos[4] = new Movie(this, “seks.mov”);
videos[5] = new Movie(this, “kirmizi.mov”);
videos[6] = new Movie(this, “animasyon.mov”);

for(int i = 0; i<videos.length; i++)
{
videos[i].stop();
videos[i].jump(0);
lengths[i] = int(videos[i].duration());
}

videos[current].play();

println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
command = “”;
inByte = ‘ ‘;
}

Processing Code

Bu hafta RC-5 kod çözücüden gelen veriyi processingde görmeyi ve bir veri geldiğinde video oynatma komutunu vermeye çalıştım. Farklı tuşlara basınca usb porttan farklı veriler geldiğini algılayabiliyor. Klavyeden de farklı tuşlara basıldığında farklı videolar oynatabiliyor. 3. kodda da 2sini bir araya getirmeye çalıştm. Şu anda kumandadan felen veriyi algılayıp farklı tuşlara basınca farklı videoları açıyor. Fakat videoyu oynatmıyor. Tuşa bastıkça frameler ilerliyor. Ben de bu sorunu çözmek için processing’in forumuna üye oldum. Henüz cevap gelmedi.

Attığım soru:

Hi,

I have a problem with this code. I have a remote control and I can send information from the usb port by using a RC-5 code receiver. I can see that different data is sent once I press different buttons. Then I want to play a certain video when I press each button. For example I want to play video 1 when I press button 1 and video 2 when I press button 2.
I found these codes; I can play the video with this code.
———————————————————–
import processing.video.*;
Movie mov_1,mov_a,nowPlaying;
float f;
float movSpeed;
void setup(){
  size(640,480,P2D);
  background(0);
  mov_1 = new Movie(this, “tation.mov”);
  mov_a = new Movie(this, “drawing.mov”);
  nowPlaying = mov_1;
  nowPlaying.loop();
}
void draw(){
  background(0);
  image(nowPlaying,0,0,width,height);
}
void movieEvent(Movie _mov){
  _mov.read();
}
void keyPressed(){
  if (key == ‘a’){
    nowPlaying = mov_1;
    nowPlaying.jump(0);
    nowPlaying.loop();
  }
  if (key == ‘b’){
    nowPlaying = mov_a;
    nowPlaying.jump(0);
    nowPlaying.loop();
  }
}
——————————————————
With the second code I can see that there is a different data that is sent from the serial port
——————————————————
import processing.serial.*;
Serial myPort;  // The serial port:
void setup() {
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
  while (myPort.available() > 0) {
    char inByte = myPort.readChar();
    println(inByte);
  }
}
—————————————————-
Then I combined these two codes to be able to play a video once there is a different information sent ( different buttons are pressed).
————————————————–
import processing.serial.*;
import processing.video.*;
Serial myPort;  // The serial port:
Movie mov_1,mov_a,nowPlaying;
float f;
float movSpeed;
void setup() {
  size(640,480,P2D);
  background(0);
  mov_1 = new Movie(this, “tation.mov”);
  mov_a = new Movie(this, “drawing.mov”);
  nowPlaying = mov_1;
  nowPlaying.loop();
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
  while (myPort.available() > 0) {
    char inByte = myPort.readChar();
    println(inByte);
    if (inByte == ‘1’) {
     nowPlaying = new Movie(this, “tation.mov”);
     nowPlaying.loop();
    }
     if (inByte == ‘2’) {
     nowPlaying = mov_a;
     nowPlaying.loop();
    }
     background(0);
     image(nowPlaying,0,0,width,height);   
    }
}
————————————————–
But I have a problem with this code,
I can see that different data is sent from the remote control from the serial monitor. Once I press button 1, video 1 appears and the same happens for button 2 and video 2. However I can only see the first frame of the videos. When I press again I see another frame. I need the videos to play in a loop. How can I solve this problem?
Thanks a lot.