Help needed with Sound

Hi all,

I'm currently having some issues with having sound on my piece.

I want it so that whenever I press inside a hotspot, sound plays, and then when the mouse is released from the hotspot, the sound stops.

The issue, however, is that if the very first click is outside of the hotspot, Processing attempts to stop sound that has not yet been loaded and hasn't played yet. If the very first click is inside the hotspot, the sound loads and plays and can stop as I want it to do.

The code is as follows :

/****************************************************
  * WHITENOISE
  * by Hendrix Nash
  * 07.05.14
  *
  * 
  * 
  ***************************************************/
                                                                                        //
import ddf.minim.*;

AudioPlayer player ;
Minim noise; 

boolean isMouseDown ; 

int rectangleLocationX , rectangleLocationY ;
int rectangleWidth , rectangleHeight ;
int whiteRectWidth , whiteRectHeight ;
int whiteRectsDist ;
int blankRectLocationX , blankRectLocationY ;
int blankRectWidth , blankRectHeight ;
int textLocationX , textLocationY ;
int intersectLeft , intersectRight , intersectTop , intersectBottom ;
String credit = "H E N D R I X N A S H // W H I T E N O I S E" ;

void setup() {
  size ( 500 , 500 ) ;
  frameRate ( 8 ) ;
  background ( 255 ) ;
  colorMode ( RGB , 255 , 255 , 255 , 100 ) ; 
  rectMode ( CENTER ) ; 
  loadFont ("Raleway-Medium-14.vlw") ;

  rectangleLocationX = width/2 ;
  rectangleLocationY = rectangleLocationX ;
  rectangleWidth = 400 ;
  rectangleHeight = rectangleWidth ;
  whiteRectWidth = rectangleWidth/2 ; 
  whiteRectHeight = whiteRectWidth ; 
  whiteRectsDist = 10 ;
  blankRectLocationX = width/2 ;
  blankRectLocationY = 475 ;
  blankRectWidth = width ;
  blankRectHeight = 50 ;
  textLocationX = width/2 ;
  textLocationY = 475 ;
  intersectLeft = ( width/2 ) - whiteRectWidth/2 ;
  intersectRight = ( width/2 ) + whiteRectWidth/2 ;
  intersectTop = ( height/2 ) - whiteRectHeight/2 ;
  intersectBottom = ( height/2 ) + whiteRectHeight/2 ;
}


void draw() {
  fill ( random(255) , random(255) , random(255) , random(75) ) ;
  rect ( rectangleLocationX , rectangleLocationY , rectangleWidth , rectangleHeight ) ;

  if ( isMouseDown == false ) {
    noFill() ;
    stroke ( 255 ) ;
    rect ( width/2, height/2, whiteRectWidth , whiteRectHeight ) ;
  }  
  else {
    int counter = 0 ; 
    int loops = 20 ;

    while ( counter < loops ) {    
      int rectangleLocationY = 155 + counter * whiteRectsDist ; 
      int shakeAmount = 10 ;
      int addX = ( int ) random ( -shakeAmount , shakeAmount ) ;
      int addY = ( int ) random ( -shakeAmount , shakeAmount ) ;

      rect ( rectangleLocationX + addX , rectangleLocationY + addY , 200 , 5 ) ;
      counter++ ; 
    }
  }

  fill ( 255 ) ;
  rect ( blankRectLocationX , blankRectLocationY , blankRectWidth , blankRectHeight ) ;

  fill ( 200 ) ;
  textSize ( 14 ) ;
  textAlign ( CENTER ) ;
  text ( credit , textLocationX , textLocationY ) ;  


}

void mousePressed() {
  isMouseDown = detectIntersection() ;     
  if (isMouseDown == true) {
   noise = new Minim ( this ) ;
   player = noise.loadFile ( "whiteNoise.wav" , 1500 ) ;
   player.play() ;
  }

}

void mouseReleased() {
  isMouseDown = false ; 
  noise.stop();

}

boolean detectIntersection() {
  boolean isInside = false ;

  if ( mouseX > intersectLeft && mouseX < intersectRight ) {
      if ( mouseY > intersectTop && mouseY < intersectBottom ) {
        isInside = true ;
      }
  }
  return isInside;
}

If anybody is able to help, I'd appreciate it so much. Thanks in advance

Answers

Sign In or Register to comment.