处理:如何使触发的声音停止?

时间:2017-03-14 21:05:47

标签: audio processing

当女人的照片移动到其中一个移动的六边形上时,会发生一些事情。我有一个声音(警察),当她接触其中一个时触发。问题是,即使她不在六边形上,声音也会持续。另外,如果她再次触摸六边形,则会在第一个声音上再次触发声音。所以现在我有一堆相同的声音互相播放。

我希望她移过六边形,声音被触发。当她从六边形移开时,声音立即停止,直到她再次移过它。 这是代码:

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim;
AudioSample police;
color underhex = color(255, 0, 0);

PImage woman;

float headX = 50, headY = 50;
float ss = 0;
float fade=30, fade2=9;

PShape hexagon, trapezoid, trapezoid2;
float[] hspeed = {2.4, 2.8, 3.2, 3.6, 4, 4.4, 4.8};
float[] g = {0, 0, 0, 0, 0, 0, 0};

void setup() {
  size(900, 600, P3D);
  background(250);
  noStroke();
  minim = new Minim(this);
  police = minim.loadSample("PoliceSiren.mp3", 1024);

  hexagon = createShape(GROUP);
  PShape trapezoid = createShape(QUAD, 100, 50, 325, 50, 375, 180, 50, 180);
  PShape trapezoid2 = createShape(QUAD, 50, 180, 375, 180, 325, 320, 100, 320);
  hexagon.scale(0.25);
  hexagon.addChild(trapezoid);
  hexagon.addChild(trapezoid2);
  hexagon.setFill(color(255, 0, 0, 50));

  woman = loadImage("woman.png");
}

void draw() {
  background(250);

  image(woman, headX, headY, 70, 81);

  for (int p = 0; p < 5; p++) {
    pushMatrix();
    translate(g[p], 120*p);

    underhex = get(int(g[p])+30, int(120*p)+30);

    shape(hexagon, 0, 0);
    //ellipse(width/2,height/2,50,50);
    popMatrix();
    g[p]+=hspeed[p];

    if (int(g[p]) > 830 || int(g[p]) < 0) {
      hspeed[p] *= -1;
    }

    if (red(underhex) < 20 && green(underhex) < 20 && blue(underhex) < 20) {
      println("she's here"+random(5));
      fill(0, 240);
      rect(0, 0, 900, 600);
      strokeWeight(30);
      fill(0, 0, 255, fade);
      stroke(0, 0, 255, fade);
      ellipse(250, 200, 100, 100);

      fill(255, 0, 0, fade);
      stroke(255, 0, 0, fade);
      ellipse(290, 330, 100, 100);

      fill(0, 0, 255, fade);
      stroke(0, 0, 255, fade);
      ellipse(680, 200, 100, 100);

      fill(255, 0, 0, fade);
      stroke(255, 0, 0, fade);
      ellipse(640, 330, 100, 100);
      noStroke();
      police.trigger();
      fade += fade2;
      if (fade<0 || fade>255) {
        fade2 *= -1;
      }
    }
  }

  if (keyPressed) {
    if (keyCode == UP) {
      headY-=random(2, 6);
    } else if (keyCode == DOWN) {
      headY+=random(2, 6);
    } else if (keyCode == LEFT) {
      headX-=random(2, 6);
    } else if (keyCode == RIGHT) {
      headX+=random(2, 6);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试使用AudioPlayer代替AudioSample。它提供isPlaying(),方便检查声音是否已播放。 e.g。

//at the top
AudioPlayer police;
//in setup()
police = minim.loadFile("PoliceSiren.mp3");
//in draw() instead of police.trigger()
if(!police.isPlaying() || ){
   police.play();
}

<强>更新

听起来像循环是你之后的事情:

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Minim minim;
AudioPlayer police;
color underhex = color(255, 0, 0);

PImage woman;

float headX = 50, headY = 50;
float ss = 0;
float fade=30, fade2=9;

PShape hexagon, trapezoid, trapezoid2;
float[] hspeed = {2.4, 2.8, 3.2, 3.6, 4, 4.4, 4.8};
float[] g = {0, 0, 0, 0, 0, 0, 0};

void setup() {
  size(900, 600, P3D);
  background(250);
  noStroke();
  minim = new Minim(this);
  //police = minim.loadSample("PoliceSiren.mp3", 1024);
  police = minim.loadFile("PoliceSiren.mp3");

  hexagon = createShape(GROUP);
  PShape trapezoid = createShape(QUAD, 100, 50, 325, 50, 375, 180, 50, 180);
  PShape trapezoid2 = createShape(QUAD, 50, 180, 375, 180, 325, 320, 100, 320);
  hexagon.scale(0.25);
  hexagon.addChild(trapezoid);
  hexagon.addChild(trapezoid2);
  hexagon.setFill(color(255, 0, 0, 50));

  woman = loadImage("woman.png");
}

void draw() {
  background(250);

  image(woman, headX, headY, 70, 81);

  for (int p = 0; p < 5; p++) {
    pushMatrix();
    translate(g[p], 120*p);

    underhex = get(int(g[p])+30, int(120*p)+30);

    shape(hexagon, 0, 0);
    //ellipse(width/2,height/2,50,50);
    popMatrix();
    g[p]+=hspeed[p];

    if (int(g[p]) > 830 || int(g[p]) < 0) {
      hspeed[p] *= -1;
    }

    if (red(underhex) < 20 && green(underhex) < 20 && blue(underhex) < 20) {
      println("she's here"+random(5));
      fill(0, 240);
      rect(0, 0, 900, 600);
      strokeWeight(30);
      fill(0, 0, 255, fade);
      stroke(0, 0, 255, fade);
      ellipse(250, 200, 100, 100);

      fill(255, 0, 0, fade);
      stroke(255, 0, 0, fade);
      ellipse(290, 330, 100, 100);

      fill(0, 0, 255, fade);
      stroke(0, 0, 255, fade);
      ellipse(680, 200, 100, 100);

      fill(255, 0, 0, fade);
      stroke(255, 0, 0, fade);
      ellipse(640, 330, 100, 100);
      noStroke();
      if(!police.isPlaying()){
        police.loop();
      }
      fade += fade2;
      if (fade<0 || fade>255) {
        fade2 *= -1;
      }
    }else{
      if(police.isLooping()){
        police.pause();
      }
    }
  }

  if (keyPressed) {
    if (keyCode == UP) {
      headY-=random(2, 6);
    } else if (keyCode == DOWN) {
      headY+=random(2, 6);
    } else if (keyCode == LEFT) {
      headX-=random(2, 6);
    } else if (keyCode == RIGHT) {
      headX+=random(2, 6);
    }
  }
}

同时结帐mute()unmute()isMuted():也许您可以保持声音循环但静音,只有在碰撞发生时才能取消静音