首页 > 解决方案 > In Processing - 一个声音文件贯穿整个动画,但只有一个类对象与声音交互

问题描述

我对Processing还是很陌生,我正在尽我所能弄清楚这个项目:(

我正在尝试创建一个序列动画,其中一个 soundFile 在整个动画中播放。然而,只有第二类需要有声音响应元素。

所有三个类对象都可以在它们自己的草图文件中单独工作。但不是当他们在一个草图中作为班级。

现在我只有第二类的声音文件,因为我不知道如何拥有相同的声音文件但响应不同的类。

当不同的课程需要打开和关闭背景时,我不确定背景是如何工作的。我也不确定我的排序方式是否正确......我将不胜感激。感谢你们!

One objectone;
Two objecttwo;
Three objectthree;

int myAnimationCounter = 0; //set myAnimationCounter incerment to 0, so allows to reset to 0 to start the animation.
int myAnimationCounter1 = 0;
int myAnimationCounter2 = 0;//set a parallel myAnimationCounter incerment to 0.
boolean startAnimation = false;  //Boolean switch to control starting animation.
boolean openingAnimation = false;

void setup() {
  size(displayWidth, displayHeight); //sketch present
  frameRate(50);
  objectone=new One();
  objecttwo=new Two();
  objectthree=new Three();
}

void draw() {
  
  if (openingAnimation) { //start the opening seq when press spacebar
    if ( myAnimationCounter>0 && myAnimationCounter<750) {
      objectone.show1();
    }
    myAnimationCounter++;
  }
  
  //when clicking the mouse 'openingAnimation' stops, playing 'startAnimation'
  
  if (startAnimation) {        
    if (myAnimationCounter1 > 0  && myAnimationCounter1 < 2500) {
        objecttwo.update2();
        objecttwo.show2();
      } else
      
        if (myAnimationCounter1 > 2500  && myAnimationCounter1 < 3000) {
objectthree.update3();                  
objectthree.show3();
        }
    myAnimationCounter1++;

//rect shape drawing at the bottom of the screen showing the playing progression of the animation

    if (myAnimationCounter2 > 0 && myAnimationCounter2 < 9000) { //independent animation through out the whole animation 
      fill(d);
      noStroke();
      rect(width, height-10, 10, width/myAnimationCounter2);
    }
    myAnimationCounter2++;
  }
}//end of draw


void keyPressed() {
  if (key == ' ') {
    openingAnimation = true;
  }
}

void mousePressed() {
  if (mousePressed) {
    startAnimation = true;
    openingAnimation=false;

    myAnimationCounter=0;
    myAnimationCounter1=0;
    myAnimationCounter2=0;
  }
class One {
  void show1() {
 background(bg);
}
}
class Two {
  import processing.sound.*;
  SoundFile loadSound; 
  Amplitude measureVol;
 void update2() {
    loadSound = new SoundFile(this, "music.mp3"); 
    loadSound.loop();  
    measureVol = new Amplitude(this);
    measureVol.input(loadSound);
  } 
  void show2() {
   float howLoud = measureVol.analyze();
}
}
class Three {
 void update3() {
  background(0);
}
 void show3() {
}
}

标签: javaclassaudioprocessing

解决方案


你可以把你的声音代码放在它自己单独的类中。然后,将类实例化并作为参数Setup注入到类中。Two确保用于提供音频功能的此类具有您希望从内部调用的操作的公共方法Two

例如:

class MyAudioClass {

SoundFile loadSound; 
Amplitude measureVol;

    public MyAudioClass() {
        measureVol = new Amplitude(this);
    }

    public void loadSound(String soundFileName) {
        loadSound = new SoundFile(this, soundFileName);
        measureVol = input(loadSound);
    }

    public void playSound() {
        loadSound.loop();
    }

    public float getHowLoud() {
        return measureVol.analyze();
    }
}

然后,在Two

class Two {
    private MySoundClass mySoundClass;
  
    // using Constructor Dependency Injection
    public Two(MySoundClass mySoundClass) {
        this.mySoundClass = mySoundClass;        
    }

    public float showHowLoud() {
        return mySoundClass.getHowLoud();
    }

(注意:我没有使用Processing,我只是根据最佳猜测转换你写的东西——所以可能需要一些调整。)

IDK 如果您还需要将类注入到OneorThree中。也许您只需要开始播放它Setup?只要您处理与您所在的实例相同的实例SetupTwo或您注入的任何其他类),事情就应该没问题。

这种编码模式通常被称为依赖注入或控制反转的一个例子,非常有用。一种变体是使用 setter 方法而不是构造函数来传递您的声音资源类。

控制反转——DI背后的概念

这表明一个类不应静态配置其依赖项,而应由外部的某个其他类配置。

这是 SOLID 的第五条原则——鲍勃叔叔的面向对象编程和设计的五个基本原则——它指出一个类应该依赖于抽象而不是具体化(简单来说,是硬编码的)。

根据这些原则,一个类应该专注于履行其职责,而不是创建它需要履行这些职责的对象。这就是依赖注入发挥作用的地方:它为类提供所需的对象。

以上内容摘自文章A Quick Intro to Dependency Injection


推荐阅读