【问题标题】:How to let a sound play once in Processing?如何让声音在处理中播放一次?
【发布时间】:2020-06-23 15:00:09
【问题描述】:
import processing.serial.*;
import processing.sound.*;

SoundFile file;
Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
//String antwoord = "A";
void setup()
{
  size(300,300);
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = trim( myPort.readStringUntil(ENTER) );
       // read it and store it in val
  } 
//println(val); //print it out in the console
file = new SoundFile(this,"Promise.mp3");

if ("A".equals(val) == true && file.isPlaying() == false) {
 

file.play();
file.amp(0.2);}

else{
 ellipse(40,40,40,40);
}
}

我收到了此代码,但我希望只要发出信号“A”,声音就会一直播放。现在它开始不断地播放,这会导致奇怪的静态噪音。怎样才能让它稳定播放?

【问题讨论】:

    标签: java audio arduino processing


    【解决方案1】:

    您在每次运行 draw 时都会创建一个新的 SoundFile。所以file.isPlaying() 将永远返回false。如果您还没有创建一个新的SoundFile,请只创建一个。最简单的解决方案可能是将file = new SoundFile(this,"Promise.mp3"); 移动到setup

    或者你检查或记住你是否已经加载了文件。

    【讨论】:

    • 如果我要使用 if 让它再次暂停?我该怎么做?因为它一直在暂停?
    • 你想什么时候暂停?目前您甚至无法正确播放它,因为您立即加载并再次播放它
    • 好吧,我想我必须解决它!谢谢!
    【解决方案2】:

    如果它有点断开连接,我很抱歉,但我建议使用 minim 或不同的声音库而不是处理库,因为它会在导出时导致很多问题(至少它对我来说一直如此)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多