在处理中播放多个视频 [英] Playing multiple videos in processing

查看:63
本文介绍了在处理中播放多个视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理并希望自动一个接一个地播放多个视频.下面是我当前的代码,但是当第一个视频播放完毕后,下一个视频不会自动开始.希望你能帮忙.

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();
}

推荐答案

您正在解决时间问题.time() 函数似乎有点问题,它似乎永远不会达到 duration() 值.现在,一个快速的解决方法是在控制台中打印 time() 和 duration() 并使用 duration() 和 last time() 值之间的差异来计算偏移量,这将使电影播放完成条件通过.

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.

我想从代码开始.开始也没关系,它可以更短,更容易管理.目前您的代码中有很多重复(参见 D.R.Y.),但这可以通过 for 循环和数组来避免.

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 循环

这些并不像听起来那么可怕.

These aren't as scary as they sound.

循环让您可以轻松地重复一个动作,无论它是什么.语法看起来有点复杂,但一旦您意识到只有 3 个部分,就不会这么复杂了.想象一下,以一定数量的步数从位置 A 到位置 B.第一个位置 A 是您初始化 for 循环的值:告诉计算机从哪里开始 countint.然后传递位置 B,即要停止循环的值.3d 和最后一个成分是增量:您将从 A 移动到 B 的步数.

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
}

例如,让我们从 0 数到 9:

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

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

将 for 循环计数器变量命名为 i 是常见的做法,但这只是一种约定.只要您保持一致,请随意命名任何对您更有意义的名称:

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);

}

好的,这就是 for 循环.还不错,而且它们会很有趣,尤其是当你开始的时候绘制图案/网格/等.在屏幕上.

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.

数组

数组是一种结构,它允许您以相同的名称存储多个值的跟踪.例如,如果使用一个简单的整数变量存储单个整数值,使用整数数组,您可以将一堆同名的整数值分组.

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.

声明数组的语法与你通常用来声明变量的语法类似(毕竟它只是一种不同类型的变量).有一些区别:- 需要在数组类型后使用 []- 您需要使用 new 键盘并指定数组/列表的大小

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;

您可以像这样声明一个整数数组(假设为 10):

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

 int[] tenInts = new int[10];

[] 符号的作用在于它不仅用于告诉 java 它正在处理一个数组,但它允许您检索和设置数组中的元素.这是使用索引完成的,数组从 0 开始索引元素.所以要访问数组中的第一个元素,您将使用 tenInts[0]这是因为我们目前有一个零数组.假设您想将第 3 个元素设置为 1.这将是索引 2,因此您可以使用它来访问数组元素并设置其值:

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");
  }
}

这篇关于在处理中播放多个视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆