gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:47:06 25 4
gpt4 key购买 nike

当女人的照片移过一个移动的六边形之一时,就会发生一些事情。当她触摸其中之一时,我会触发声音(警察)。问题是,即使她不在六角形上的时间更长,声音仍会持续。另外,如果她再次触摸六边形,则会在第一个声音之上再次触发声音。因此,现在我在彼此播放相同的声音时一团糟。

我要她移到六边形上,然后触发声音。当她移离六角形时,声音会立即停止,直到她再次移过它。
这是代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com