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

标签 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);
    }
  }
}

最佳答案

尝试使用AudioPlayer而不是AudioSample。它提供了isPlaying(),可以方便地检查声音是否已经在播放。
例如

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

还要 check out mute()unmute()isMuted():也许您可以保持声音循环,但保持静音,并且仅在发生碰撞时取消静音

关于audio - 处理:如何使触发的声音停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796535/

相关文章:

audio - 在关闭声音的情况下嵌入 YouTube

android - 录制音频并保存到应用程序的数据文件夹中

bash - 如何在没有GUI的情况下通过Debian中的Bash用VLC流式传输音频?

html - 如何通过文件 ://protocol 从远程文件播放音频

java - 在处理(java)编码中使用数组或类的数字网格

python - saveFrame 产生 NullPointerException 错误

ios - Sinch 在 iOS 中隐藏 webview 音频

java - 在处理方法中使用 CreateGraphics 输出多页 PDF

java - 求逆矩阵

java - 在 Eclipse 中调试多个同名变量