2013-07-09 18 views
1

這裏只播放一個音頻文件,同時是我:如何微量

2個按鈕

2聽起來

當按下按鈕1我想聽起來1時按鈕2按下發揮我只想要sound2播放。

我的代碼:

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

我創建了我自己的圖形用戶界面 – gJg

回答

0

更新::

我解決了這個由

添加IF & 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(); 
     }  
    }  
0

您必須先停止當前樣本,然後再啓動另一個樣本。
如果你要玩player_2,你必須調用:

player_1.pause(); 
player_1.rewind(); 

你打電話player_2.play();和周圍的其他方法之前。

+0

Pwdr謝謝你,我嘗試了這種方式,就像你說的那樣,但是不管我做什麼,它只能播放第二個球員?還有什麼可以建議的嗎? – gJg

0

我做了這個非常愚蠢的例子,它有什麼幫助嗎?這是愚蠢的,特別是按鈕類和事件處理,但是因爲我不知道你正在使用哪個RadioButton,所以我把這些線放在一起......也許它可以激勵你。

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

} 
+0

謝謝,這或多或少是我用過的。 – gJg