Playing multiple videos in processing

蓝咒 提交于 2019-12-04 02:32:16

问题


I am working with processing and would like to play multiple videos one after the other automatically. Below is my current code but when the first video finishes playing the next video does not automatically begin. hope you can help.

import processing.video.*;
Movie myMovie1, myMovie2, myMovie3, myMovie4, myMovie5, myMovie6;
boolean playMovie1=true;
boolean playMovie2=false;
boolean playMovie3=false;
boolean playMovie4=false;
boolean playMovie5=false;
boolean playMovie6=false;

void setup(){
size(800,500);
myMovie1 = new Movie(this, "ch1.mp4");
myMovie2 = new Movie(this, "ch2.mp4");
myMovie3 = new Movie(this, "ch3.mp4");
myMovie4 = new Movie(this, "ch4.mp4");
myMovie5 = new Movie(this, "ch5.mp4");
myMovie6 = new Movie(this, "ch6.mp4");


}

void draw(){
background(0);
if(playMovie1==true){

myMovie1.play();
image(myMovie1,0,0);
if(myMovie1.time()>=myMovie1.duration()){
myMovie1.stop();
playMovie1=false;
playMovie2=true;
}
}

if(playMovie2==true){

myMovie2.play();
image(myMovie2,0,0);
if(myMovie2.time()>=myMovie2.duration()){
myMovie2.stop();
playMovie2=false;
playMovie3=true;
}
}

if(playMovie3==true){

myMovie3.play();
image(myMovie3,0,0);
if(myMovie3.time()>=myMovie3.duration()){
myMovie3.stop();
playMovie3=false;
playMovie4=true;
}
}

if(playMovie4==true){

myMovie4.play();
image(myMovie4,0,0);
if(myMovie4.time()>=myMovie4.duration()){
myMovie4.stop();
playMovie4=false;
playMovie5=true;
}
}

if(playMovie5==true){

myMovie5.play();
image(myMovie5,0,0);
if(myMovie5.time()>=myMovie5.duration()){
myMovie5.stop();
playMovie5=false;
playMovie6=true;
}
}


if(playMovie6==true){
myMovie6.play();
image(myMovie6,0,0);

if(myMovie6.time()>=myMovie6.duration()){
myMovie6.stop();
playMovie6=false;
}  
}
}

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

回答1:


You're about the problem with time. The time() function seems a bit buggy and it never seems to reach the duration() value. For now a quick work around would be to print the time() and duration() in the console and use the difference between the duration() and last time() value to workout the offset that will make the movie playback complete condition pass.

I'd like to start with the code. It's ok too start with, it can shorter and easier to manage. At the moment there's a lot of repetition in your code (see D.R.Y.), but this can be avoided with for loops and arrays.

for loops

These aren't as scary as they sound.

Loops allow you to easily repeat an action, whatever it may be. The syntax looks a bit complicated, but it's not once you realise there are only 3 parts. Imagine going from position A to position B in a certain number of steps. This first position A is the value you initialize a for loop with: tell the computer where to start countint from. Then you pass position B, the value you want to stop the loop at. The 3d and last ingredient is the increment: in how many steps will you move from A to B.

The syntax looks a bit like this:

for (start ; stop; steps){
  //do something here
}

for example, let's count from 0 to 9:

for(var start = 0; start < 10; start = start + 1){
  println(start);
}

It's common practice to name your for loop counter variable i, but that's just a convention. Feel free to name it whatever makes more sense to you, as long as you're being consistent:

for(int i = 0 ; i < 10 ; i++){

  println(i);

}

Ok, so that's for loops. Not too bad and and they can be a lot of fun, especially when you start drawing patterns/grids/etc. on the screen.

arrays

An array is a structure that allows you store an keep track of multiple values under the same name. For example, if with a simple integer variable you store a single integer value, with an integer array, you can group a bunch of integer values under the same name.

The syntax for declaring an array looks similar to the one you normally use to declare a variable (it's just a different type of variable after all). There are a few differences: - you need to use [] after the array type - you need to use the new keyboard and specify the size of your array/list

so if you declared an integer like so:

 int i = 0;

you would declare an array of integers (let's say 10) like so:

 int[] tenInts = new int[10];

The thing with the [] symbols is that it's not ontly used to tell java that it's dealing with an array, but it allows allows you retrieve and set elements within the array. This is done using an index and arrays start indexing elements from 0. So to access the first element in the array you would use tenInts[0] This will be since we have an array of zeros at the moment. Let's say you want to set the 3rd element to 1. This would be index 2, so you can use that to access the array element and set it's value:

println("3rd element: " + tenInts[2]);//will print 0
tenInts[2] = 1;
println("3rd element: " + tenInts[2]);//will print 1

Now you've got all the ingredients to put this together without repeating too much code:

import processing.video.*;

int numMovies = 6;//total number of movies
Movie[] playlist = new Movie[numMovies];//a list of all the movie objects, currently not initialized
int currentMovieIndex = 0;//index of the movie currently playing

float movieEndDuration = 0.029719;//a 'magic number' helpful to find out when a movie finishes playing

void setup(){
  size(800,500);
  for(int i = 0 ; i < numMovies; i++){
    //initialize each movie object in the list
    playlist[i] = new Movie(this,"transit.mov");//new Movie(this, "ch"+(i+1)+".mp4");
  }
  //start playback
  playlist[currentMovieIndex].play();
}

void draw(){
  background(0);
  image(playlist[currentMovieIndex],0,0);
}

void movieEvent(Movie m){
  m.read();
  //handy for debugging and figuring out the 'magic number'
  println(m.time() + " / " + m.duration() + " / " + (m.time() + movieEndDuration));
  //hacky check movie end 
  if((m.time() + movieEndDuration) >= m.duration()){
      println("movie at index " + currentMovieIndex + " finished playback");
      //go to the next movie index
      currentMovieIndex = (currentMovieIndex+1) % numMovies;//increment by one buy use % to loop back to index 0 when the end of the movie array is reached
      //use this to tell the next movie in the list to play
      playlist[currentMovieIndex].play();
      println("movie at index " + currentMovieIndex + " started");
  }
}


来源:https://stackoverflow.com/questions/31832499/playing-multiple-videos-in-processing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!