Using a webcam to trigger off sound

Hi there. I am new to Processing.
I am having problems getting a single sound to trigger when there is movement using a webcam as a motion sensor and when there is no movement the sound stops.

My main problem is the sound keeps on repeating or overlapping when there is movement, rather than starting from its current pause position or playing a single sound.

I am also having problem with the the sound library,

I am using 3.4. It does not seem to understand methods like pause() (does not exist error) and isPlaying(). (cannot convert from int to boolean error).

Here is my code, that I have borrowed and hacked.

Any help would be appreciated.

import processing.video.*;
import processing.sound.*;  
Capture video;
 
PImage prevFrame;
 
float threshold = 150;
 
int ballX = width/8;
int ballY = height/8;
int rsp = 5;

boolean a = false;
SoundFile musicfile;

 
void setup() {
  size(320, 240);
  video = new Capture(this, width, height, 30);
  video.start();
  prevFrame = createImage(video.width, video.height, RGB);
  musicfile = new SoundFile(this, "Sydney_Good.wav"); // load sound file
}
 
void draw() {
 
 
  if (video.available()) {
 
    prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height); 
    prevFrame.updatePixels();
    video.read();
  }
 
  loadPixels();
  video.loadPixels();
  prevFrame.loadPixels();

  float totalMotion = 0;
 
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;            
      color current = video.pixels[loc];      
      color previous = prevFrame.pixels[loc]; 
      float r1 = red(current); 
      float g1 = green(current); 
      float b1 = blue(current);
      float r2 = red(previous); 
      float g2 = green(previous); 
      float b2 = blue(previous);
      float diff = dist(r1, g1, b1, r2, g2, b2);
     totalMotion += diff;
    }
  }
  float avgMotion = totalMotion / video.pixels.length;
  
  if (avgMotion >= 40) {
    // turn on sound
  noStroke();
  fill(0, 0, 255);
  ellipse(ballX+40, ballY+40, 90, 90);
  a = true;
  playSound();
  }
  
   if (avgMotion <= 5) { 
  // turn off sound
  fill(0, 0, 155);
  ellipse(ballX+40, ballY+40, 90, 90);
  a = false;
  playSound();
  }
}

void playSound() {
  if (a) {
    if (musicfile.isPlaying() )
   {
    musicfile.pause();
    }
    else
    {    
      musicfile.loop();
    }
  }  
  
  if (!a) {

   musicfile.pause();
   
  } 
}

Keep in mind that the music library and the sound library are different things. They have their own methods on their own objects. Minim is another common sound library to use – it also has its own methods on its own objects.

Look at the examples in the examples folder to see working sketches that play videos or sound files. Then try modifying them. Be very careful about mixing-and-matching examples from two different librarires, as initially you may get confused. Better to get the two parts of the project working separately, then merge them into a new combined sketch.