Java 处理-根据按下的鼠标播放不同的音频

Java 处理-根据按下的鼠标播放不同的音频,java,if-statement,audio,processing,Java,If Statement,Audio,Processing,我正在尝试根据单击处理草图的哪个区域来播放不同的音频文件。我的声音文件有问题,在单击第一个区域时,甚至只能播放一个文件 我已导入声音库,但我必须有语法或目录错误。这是我一直得到的信息: 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

我正在尝试根据单击处理草图的哪个区域来播放不同的音频文件。我的声音文件有问题,在单击第一个区域时,甚至只能播放一个文件

我已导入声音库,但我必须有语法或目录错误。这是我一直得到的信息:

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");
  }
}
用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");
  }
}
用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");
  }
}

你真的不应该像那样在鼠标里加载文件。取而代之的是,将它们全部加载到安装程序中,然后在需要时引用它们。下面是一个例子:

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

你真的不应该像那样在鼠标里加载文件。取而代之的是,将它们全部加载到安装程序中,然后在需要时引用它们。下面是一个例子:

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