在处理中播放视频

Playing video in Processing

我正在用 Arduino 读取电容式传感器,触摸的传感器的数量被传送给 Processing,目前它只有 1 或 2。

在处理过程中,我正在尝试根据接收到的传感器数量播放视频。我需要能够在播放期间在不同的视频之间切换,播放结束后,如果按下相同的数字,视频应该跳回到开始。

这是处理中的代码

    import processing.serial.*;
    import processing.video.*;
    
    Movie video1, video2;
    
    Serial port;
    char in;
    char previousIn;
    boolean playing = false;
    //float time = 0;
    
    void setup() {  
      fullScreen(JAVA2D);
      frameRate(25);
      video1 = new Movie(this, "redFIN.mp4");
      video2 = new Movie(this, "greenFIN.mp4");
      port = new Serial(this, Serial.list()[0], 9600);
    }
    
    void movieEvent(Movie m) {
      m.read();
    }
    
    void draw() {  
      if ( port.available() > 0) {  // If data is available,
        in = char(port.read());
        print(in);
      }
    
      if (in == '1') {  
        video1.play();
        video2.stop();
        in = previousIn;
        if (in == previousIn) {
          video1.jump(0);
        }
      }
      image(video1, 0, 0, width, height);
    
    
      if (in =='2') {
        video2.play();
        video1.stop();
        in = previousIn;
        if (in == previousIn) {
          video2.jump(0);
        }
      }
      image(video2, 0, 0, width, height);
    }

目前,我可以在视频之间切换,但只能从 movie1 切换到 movie2,当从 movie2 返回到 movie1 时,我得到了 movie1 的音频,但它一直显示 movie2 的最后一帧。

如果能深入了解为什么会这样,我将不胜感激。

你已经非常接近了,但仍有几个区域不会像你期望的那样运行:

  1. 您使用 image() 绘制了 video1video2:您只想渲染一个视频
  2. if (in == previousIn) 条件可能应该在设置 previousIn 之前发生(顺便说一句,您正在设置其他方式)。 (否则条件永远为真)

这是一个使用第三个 Movie 变量的版本,它只是根据上下文对 video1video2 的引用:

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

Movie video1, video2, currentVideo;

Serial port;
char in;
char previousIn;
boolean playing = false;
//float time = 0;

void setup() {  
  fullScreen(JAVA2D);
  frameRate(25);
  video1 = new Movie(this, "redFIN.mp4");
  video2 = new Movie(this, "greenFIN.mp4");
  // default to video1
  currentVideo = video1;
  port = new Serial(this, Serial.list()[0], 9600);
}

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

void draw() {  
  if ( port.available() > 0) {  // If data is available,
    in = char(port.read());
    print(in);
  }

  if (in == '1') {  
    video1.play();
    video2.stop();
    if (in == previousIn) {
      video1.jump(0);
    }
    previousIn = in;
    currentVideo = video1;
  }

  if (in =='2') {
    video2.play();
    video1.stop();
    if (in == previousIn) {
      video2.jump(0);
    }
    previousIn = in;
    currentVideo = video2;
  }
  
  image(currentVideo, 0, 0, width, height);
}