We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I have been coding a progress bar using some examples that I found. The bar is almost done but it doesn't constantly update. Can someone assist me in getting it to update while the video is playing? https://forum.processing.org/two/discussion/8103/using-a-controlp5-slider-as-video-progress-bar#latest `
//using processing video and movie
import processing.video.*;
import controlP5.*;
import java.awt.geom.*;
ControlP5 cp5;
Movie movie;
boolean play;
int speed = 1;
int progress;
void setup() {
size(1280, 360);
cp5 = new ControlP5(this);
background(0);
// Load and play the video in a loop
movie = new Movie(this, "video.mov");
movie.play();
movie.speed(speed);
//Progress Bar
ProgressBar p;
p = new ProgressBar(cp5, "progress");
p.setPosition(130, 345).setSize(350, 10).setRange(0, (int)
movie.duration()).listen(true);
//callback listener for Timeline object
p.addCallback(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if(theEvent.getAction() == ControlP5.ACTION_PRESS) {
float x = theEvent.getController().getPointer().x();
float a1 = 0;
float a2 = theEvent.getController().getWidth();
float b1 = theEvent.getController().getMin();
float b2 = theEvent.getController().getMax();
float val = map(x,a1,a2,b1,b2);
theEvent.getController().setValue(val);
println("now change movie time to ", val, movie.time());
movie.jump((float) val);
}
}
}
);
// a toggle to switch between play and pause mode
cp5.addToggle("play")
.setPosition(10,375)
.setSize(50,19)
.getCaptionLabel().align(CENTER,CENTER);
}
void movieEvent(Movie m) {
m.read();
}
void draw() {
if (play) {
// update the progressBar value/position
// replace with audio-track position: progress = player.position();
image(movie, width/2, 0, width/2, height);
if (movie.available()) movie.read();
progress = (int) movie.time();
}
}
class ProgressBar extends Controller<ProgressBar> {
public ProgressBar(ControlP5 theControlP5, String theName) {
super(theControlP5, theName);
setView(new ControllerView<ProgressBar>() {
public void display(PGraphics pg, ProgressBar c) {
pg.fill(!isMouseOver() ? c.getColor().getForeground():c.getColor().getActive());
pg.rect(0, 0, c.getWidth(), c.getHeight());
float val = map(c.getValue(),c.getMin(), c.getMax(), 0, c.getWidth());
pg.fill(255);
pg.rect(0, 0, val, c.getHeight());
}
}
);
}
public ProgressBar setValue(float theValue) {
return super.setValue(constrain(theValue, getMin(), getMax()));
}
public ProgressBar setRange(int theStart, int theEnd) {
_myMin = theStart;
_myMax = theEnd;
return this;
}
}
`
Answers
Your code is not complete. Please make sure the code runs. If it doesn't, please explain any errors that you are getting.
Kf
The code runs and when clicking on the progress bar, the video and the bar changes accordingly. The video doesn't constantly update. Could you point me towards the code that I am missing?
Line 48, your definition of movie, cp5, imports? I have the following below running.
Kf
Sorry I'm not quite following. I am not sure if you meant my line 48 or one of yours. Could you give me a more detailed explanation of the problem and solution?
re: kfrajer's
@Izzy280 -- on your line 48:
You use
movie
-- however the code that you pasted to the forum is incomplete; if someone cut-and-pastes it out of the forum it does not run (try it!). It does not run because you did not include definitions for movie, or cp5. You did not import the libraries (in what you shared on the forum). You also may not have included your definitions of some global variables. So nobody can test your code; it doesn't work. In order for people to help you, please correct your code to include whatever imports you are using that make it run -- kfrajer has posted an example that works for him after trying to guess what was missing for yours (and maybe making other changes, I haven't checked).Thank you very much! I edited the code accordingly. Please let me know if there are more issues when trying to run.
Ok, so you have to implement these two changes in your code:
Change #1
boolean play=true;
Change #2
(YES, you need to adjust the image() parameters and you need to remove the conditional as it is being handle by your movieEvent() function.
Your code above works: clicking on the progress bar adjusts the position of the movie.
Kf
Thank you so much! The code works perfectly!