这就是我所拥有的:
2个按钮
2声
当按钮1按下时我想按下按钮2时播放声音1我只想播放声音2。
我的代码:
import ddf.minim.*;
RadioButtons r;
boolean showGUI = false;
Minim minim;
AudioPlayer player_1;
AudioPlayer player_2;
PImage img, img2;
PShape img1;
void setup() {
size(1024,735);
String[] radioNames = {"Button1", "Button2"};
r = new RadioButtons(radioNames.length, 20,700,50,30, HORIZONTAL);
r.setNames(radioNames);
img = loadImage("img.jpg");
img1 = loadImage("img1.png");
img2 = loadShape("img1.svg");
minim = new Minim(this);
//sound1
player_1 = minim.loadFile("sound1.wav");
//sound2
player_2 = minim.loadFile("soun2d2.wav");
}
void draw() {
//background(0);
//println (mouseX +"," + mouseY);
// Draw the image to the screen at coordinate (0,0)
//sound1
image(img,0,0);
if(r.get() == 0)
shape(img1,695,106);
if(mousePressed){
if(mouseX>695 && mouseX <695+190 && mouseY>106 && mouseY <106+180){
fill(0,0,0,0);
image(img2,300,150);
player_1.cue(0);
player_1.play();
}
}
//sound2
if(r.get() == 1)
shape(img1,695,106);
if(mousePressed){
if(mouseX>695 && mouseX <695+190 && mouseY>106 && mouseY <106+180){
fill(0,0,0,0);
image(img2,300,150);
player_2.cue(0);
player_2.play();
}
}
if(showGUI)
{
r.display();
}
}
void mouseReleased()
{
if(showGUI){
if(mouseY> height-60)
r.mouseReleased();
else
showGUI = false;
}
else{
showGUI = true;
}
}
此时两个声音同时播放。
我错过了什么?
答案 0 :(得分:0)
您必须在开始另一个样本之前停止当前样本 如果你想玩player_2,你必须打电话:
player_1.pause();
player_1.rewind();
在致电player_2.play();
之前,反之亦然。
答案 1 :(得分:0)
UPDATE ::
我通过
解决了这个问题添加IF&amp; ELSE IF语句。
以下示例。
void draw() {
//background(0);
//println (mouseX +"," + mouseY);
// Draw the image to the screen at coordinate (0,0)
//sound1
image(img,0,0);
if(r.get() == 0)
{
code...
}
}
}
//sound2
else if(r.get() == 1)
{
code...
}
}
}
if(showGUI)
{
r.display();
}
}
答案 2 :(得分:0)
我做了这个非常愚蠢的例子,有什么帮助吗?它是愚蠢的,特别是按钮类和事件处理,但由于我不知道你正在使用哪些RadioButtons我把这些线放在一起......也许它可以激发你的灵感。
import ddf.minim.*;
PVector pOne, pTwo;
Button one, two;
Minim minim;
AudioPlayer player_1;
AudioPlayer player_2;
void setup() {
size(300,300);
pOne = new PVector(50, 150);
pTwo = new PVector(150, 150);
one = new Button (pOne, "one");
two = new Button (pTwo, "two");
minim = new Minim(this);
//sound1
player_2 = minim.loadFile("down_in_the_hole.mp3");
//sound2
player_1 = minim.loadFile("emotional_rescue.mp3");
}
void draw() {
background(220);
one.display();
one.update();
two.display();
two.update();
if(one.pressed){
player_2.pause();
player_1.rewind();
player_1.play();
ellipse(30,30,10,20);
// two.pressed = false;
}
if(two.pressed){
player_1.pause();
player_2.rewind();
player_2.play();
ellipse(130,30,10,20);
//one.pressed = false;
}
}
void mouseReleased(){
one.pressed = false;
two.pressed = false;
}
class Button{
String name;
PVector pos;
boolean pressed = false;
int xsz = 40;
int ysz = 20;
Button(PVector _pos, String _name ){
pos = _pos;
name = _name;
}
void display(){
color c = isOver()? 255:120;
fill(c);
rect(pos.x, pos.y, xsz, ysz);
fill(0);
text(name, pos.x+5, pos.y +ysz/2);
}
void update(){
if(isOver() && mousePressed)
pressed = true;
}
boolean isOver(){
return mouseX > pos.x && mouseX < pos.x + xsz &&
mouseY > pos.y && mouseY < pos.y + ysz;
}
}