We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I wrote this class dedicated to a video player. I want to read videos when I click on 3d spheres in my main tab. How should I proceed to choose my videos inside the videoList, (they have the same names than the spheres) with an index ? I click on a sphere, it plays the full screen video wich has the same name than the sphere, then the video stop, window closes and go back to interface with the spheres and so on... Do someone know how I should start? Thanks a lot in advance for your help ;)
class Movie {
int currentMovieIndex;
int STATE_PLAYING_SELECTED_MOVIE;
int currentState = STATE_PLAYING_SELECTED_MOVIE, STATE_WAITING_FOR_CLICK;
float movieEndDuration = 0.039719;
String [] stateLabels = {"waiting for click", "playing selected movie"};
String [] videoList= {"obscurité", "sombre"};
Movie[] movies;
Movie(movies, String[]videoList) {
Movie[] movies = new Movie[videoList.length];
for (int i=0; i < videoList.length; i++) {
movies[i] = new Movie(this, videoList[i]+".mp4");
movies[currentMovieIndex].play();
}
}
void drawMovie() {
background(0);
image(movies[currentMovieIndex], 0, 0);
}
void playSelectedMovie() {
currentState = STATE_PLAYING_SELECTED_MOVIE;
movies[currentMovieIndex].play();
}
void movieEvent (Movie m) {
if (currentState == STATE_PLAYING_SELECTED_MOVIE) {
if ((m.time()+ movieEndDuration) < m.duration) {
m.read();
movies[currentMovieIndex].play();
} else if ((m.time() +movieEndDuration)>= m.duration()) {
m.stop();
currentState = STATE_WAITING_FOR_CLICK;
println("movie at index " + currentMovieIndex + " finished playback, current state: " + stateLabels[currentState]);
}
}
}
}
Answers
Have you implemented this part? More details if possible...
Important. The name of your class is Movie. However, Processing has a class called Movie and this is just conflicting. Change the name to something my MyMovieClass, for instance.
There are a couple of considerations. Before going any further, it is very important to define exactly how movies are played. For this, you need to consider all possible scenarios in your program. Consider the following situation:
The user clicks the sphere. The movie starts playing. Playing another movie is not possible but only after the movie stops playing.
Second situation:
A user clicks in a sphere. The movie starts playing. Then the user clicks on another sphere. The current movie stops playing and the other movie starts playing.
This behavior is different and you should provide all these details upfront.
Related to your line 32. keep in mind that you need to call the PApplet's movieEvent callback to be able to use this function, like this:
Kf
This next post could be a starter: https://forum.processing.org/two/discussion/22640/can-anyone-help-me-find-what-s-wrong-with-my-code-for-a-video-player-based-off-sensor-reading#latest
Check my code posted on May 18th. It has arduino code. You can remove any reference from arduino or serial since I implemented an alternative response using key events.
You have a look at it and hopefully you can see what direction I would be taking next, or at least for you to consider.
Kf
Dear kfrajer, thank you very much for your help. I checked out your code and implemented it into my class, I still have a problem with the constructor... I indicate Movie then the list to play... ?!
To answer your question: yes if the user clicks the sphere. The movie starts playing. Playing another movie is not possible but only after the movie stops playing. And there will be an escape button if the user doesn't want to read completely the video and go back to the interface to click on a new sphere. I hope it's more clear?! I never used this library before and I am not fluent in Processing as you can notice Thanks a lot
lm
:-B
Dear kfrajer,
if you ever have a bit of time today... Thanks a lot. best, l
You don't need line 14. In that block, you are initializing the movies. Line 14 play the movies.... but all at the same time? Conclusion: not need it!
Also you have line 20 and 21, idx and current... you have two variables managing your movies? How do you manage your current variable? I am thinking it will be better to see all your code including setup and draw. I find for this type of problems it is better to be able to have a running example.
For line 45, instead of using jump(0), I believe you can use .rewind()... this is not important atm but to keep in mind, like making a mental note: Check documentation.
Kf
Dear Kf, Thanks a lot for your answer.I post you the entire code, I haven't tried to link my selectSphere function with my my Movie Class yet since I wanted this class to be written properly before :)) Sorry the code is long! Thanks a lot for your help. Lm
Dear Kf, I basically a problem with the constructor of my class MyMovieClass line 533, don't know how to solve that now... It's the 1rst time I use the video library !! Thanks for your attention and help. best, l
Ok, I can see now what you mean with testing it before merging it with your project. Do you have a hook to connect the movie to the sphere already? Just curious... but not needed right now...
Here next is a demo after I adopted the code from the previous post. I removed all the references to serial and any serial processing. Now this is how the demo works. This demo assumes you have three different movies. If you press from 1 to 3, inclusive, you activate a movie. No other movie can be played. However, if you press the button, it will stop playing the movie and it goes back to the idle stage where it can accept a numeric entry to play a movie. Notice that if you let a movie play until the end, it should go back to idle mode. However, letting the movie playing 'till the end might not work 100% correct. The issue is that it seems that the video finishes a second or two faster than sound or so it is my impression with my video samples.
This demo should get you going to your next step.
Kf
Dear Kf, Thank you very much for taking some time to help me :) I have implemented your code in my sketch, but I still need to find a way to play the videos when I press on a sphere. I'll try it later on today. If you have any idea I'd be glad to hear about it! I thought about comparing the names of the spheres to the names of the videos (they are the same) to check out if I play the right video...?
Dear Kf, I tried to implement your code, but is there a simpler way to compare my movies[idx] to my sphere number? In your code you update movies according to the sensors idx, but I have 28 videos to load/play ... If I click on my sphere and idx= sphere number then play movies[idx] ?! Hope it's clear?!!
Dear Kf,
Here is the code I've written, but I still don't use a currentMovie and when The mouse is not over a button the image of the last video remain as the background. Should I put everything in a mouseReleased function?! Thank you very much in advance for your help!
Some comments:
The class Button has a member field called currentButton. What is the purpose of that field? You should name this variable based on its functionality. Maybe you meant buttonID?
Now, you have also a currentButton. I will recommend not to use it as you are right now. Instead, keep a variable that remembers what movie is being played right now.
This last generates a new question. What is the purpose of your stop button? I can see that you start playing the movie when the mouse is on top of the button. Should the movie stop playing when the mouse is not hovering on top of the button?
Modified version below.
******* EDITED
I think the stop button shouldamke sure all the movies call the stop function so they are all rewind and ready to start playing from the start.... it should be an easy implementation below if this is the desired behavior.
Kf
Dear Kfrajer, Thank you so much!! Your code works perfectly and does exactly what I wanted that is to say as you suggest : " the movie stop playing when the mouse is not hovering on top of the button" What didn't work with my code. I understand the purpose and advantage to comprae the idx with the currentMovie and not comparing the string labels of the buttons with the names of the movies.
The only thing I don't understand and don't have a clue about is this line ?! : final int NOMOVIE=PApplet.MAX_INT; I it a practical way to use this vide oplayer later as a class inside a longer sketch?! Thanks again for your work and patience.
I think one of the reason is that you had everything inside the for loop. Also, you addressed the action when you were on top of the button but you didn't address the action when the mouse was not on any button, or you will probably doing it in an ill-constructed else statement. I just rearrange what you had. I used the indes instead bc it is more clear to me.
final int NOMOVIE=PApplet.MAX_INT
is a way to define a reference value in my sketch. Every sketch in Processing extends from the PApplet class and in the process, your sketch inherits functionalities. This is the reason you can draw an ellipse or access mouse event actions, for example. PApplet also has access to some constants like the max number an integer variable can handle. You can access this info in different ways. For example, from the java.lang.Integer class:https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.htmlHowever, I just stick to what Processing offers.
Kf
Thank you very much Kfrajer for the explanations and the link to the doc, I really appreciate your help! Here is the videoPlayer implemented and ready to use inside my main sketch (using PApplet ;))):
I won't be able to run your code but until later today to see the difference, if any. The main sketch refers to selecting the spheres in 3D space? What have you implemented so far?
Kf
Dear Kfrajer no problem, the code run properly Tomorrow I'll connect it with the main sketch that refers to spheres in 3D space... Today I tried to fix the problem of distance between the spheres. Most of the examples are the same bouncing balls in a square 3d space thatdoesn't correspond tomine, but still it's a good base to start with. See you then and thanks a lot for your feedback. best wishes, lm
Small change after revising my prev post. Lines 44 to 48 are not needed... they are indeed not effective. Removing those lines will not change the current program functionality. On a side note, the stop button does not have a clear function in your sample sketch. I suppose you are only interested in the functionality of playing movies only when hovering on a button as this can be applied to your spheres.
Related to the main sketch with the noisy spheres, I notice that sometimes I get red or green spirals in some spheres. Should all the spheres react in the same way?
Kf