2016-11-25 44 views
1

我試圖獲得不同的音頻文件播放,取決於我點擊的Processing素描的「區域」。我在soundFile上遇到問題,即使只點擊一個文件,也會點擊第一個區域。處理 - 播放不同的音頻取決於mousePressed

我導入了sound library但我必須有語法或目錄錯誤。這是我不斷收到消息:

Error: Soundfile doesn't exist. Pleae check path 
Could not run the sketch (Target VM failed to initialize). 
For more information, read revisions.txt and Help → Troubleshooting. 

首先,它可以裝載5種不同的聲音文件作爲if報表的條件?

代碼在這裏:

PImage img; // Declare variable "a" of type PImage  
import processing.sound.*; 
    SoundFile file; 


void setup() { 
    size(1475, 995); 
    // The image file must be in the data folder of the current sketch 
    // to load successfully 
    img = loadImage("PaceTaker.jpg"); // Load the image into the program 
} 

void draw() { 
    // Displays the image at its actual size at point (0,0) 
    image(img, 0, 0); 
} 
void mousePressed() { 
    if (mouseX>105 && mouseX<337 && mouseY>696 && mouseY<714) { 
    // Load a soundfile from the /data folder of the sketch and play it back 
    file = new SoundFile(this, "Heartbeatreg.mp3"); 
    file.play(); 
    stroke(0); 
    } 
    else if (mouseX>410 && mouseX<584 && mouseY>696 && mouseY<714) { 
    println("yikes2"); 
    stroke(0); 
    } 
    else if (mouseX>659 && mouseX<837 && mouseY>696 && mouseY<714) { 
    println("yikes3"); 
    stroke(0); 
    } 
    else if (mouseX>928 && mouseX<1065 && mouseY>696 && mouseY<714) { 
    println("yikes4"); 
    stroke(0); 
    } 
    else if (mouseX>1123 && mouseX<1397 && mouseY>696 && mouseY<714) { 
    println("yikes5"); 
    stroke(0); 
    } 
    else { 
    println("hello"); 
    } 
} 

回答

2

minim解決了這個。代碼如下:

PImage img; // Declare variable "a" of type PImage 
import ddf.minim.*; 
AudioPlayer player; 
Minim minim; 

void setup() { 
    size(1475, 995); 
    // The image file must be in the data folder of the current sketch 
    // to load successfully 
    img = loadImage("PaceTaker.jpg"); // Load the image into the program 
    minim = new Minim(this); 
    player = minim.loadFile("Heartbeatreg.mp3"); 
} 

void draw() { 
    // Displays the image at its actual size at point (0,0) 
    image(img, 0, 0); 
} 
void mousePressed() { 
    if (mouseX>105 && mouseX<337 && mouseY>696 && mouseY<714){ 
    if (player.isPlaying()) { 
     player.close();} 
    player = minim.loadFile("Heartbeatreg.mp3"); 
    player.play(); 
    stroke(0); 
    } 
    else if (mouseX>410 && mouseX<584 && mouseY>696 && mouseY<714) { 
    if (player.isPlaying()) { 
     player.close();} 
    player = minim.loadFile("Heartbeatflatline.mp3"); 
    player.play(); 
    stroke(0); 
    } 
    else if (mouseX>659 && mouseX<837 && mouseY>696 && mouseY<714) { 
    if (player.isPlaying()) { 
     player.close();} 
    player = minim.loadFile("Heartbeatsuperfast.mp3"); 
    player.play(); 
    stroke(0); 
    } 
    else if (mouseX>928 && mouseX<1065 && mouseY>696 && mouseY<714) { 
    if (player.isPlaying()) { 
     player.close();} 
    player = minim.loadFile("Heartbeatslow.mp3"); 
    player.play(); 
    stroke(0); 
    } 
    else if (mouseX>1123 && mouseX<1397 && mouseY>696 && mouseY<714) { 
    if (player.isPlaying()) { 
     player.close();} 
    player = minim.loadFile("Heartbeatfast.mp3"); 
    player.play(); 
    stroke(0); 
    } 
    else { 
    println("void click"); 
    } 
} 
2

你真的不應該像這樣加載mousePressed()裏面的文件。相反,將它們全部加載到setup()之內,然後在需要時引用它們。這裏有一個例子:

Minim minim; 
AudioPlayer songOne; 
AudioPlayer songTwo; 

void setup() { 
    size(1475, 995); 
    minim = new Minim(this); 
    songOne = minim.loadFile("SongOne.mp3"); 
    songTwo = minim.loadFile("SongTwo.mp3"); 
} 

void draw() { 
} 

void mousePressed() { 
    if (mouseX < width/2) { 
    songOne.rewind(); 
    songOne.play(); 
    } 
    else { 
    songTwo.rewind(); 
    songTwo.play(); 
    } 
}