Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Play song on demand

$
0
0

Hello everyone!

So, I want to have a song to start playing after having pressed a button. I've got the following code, but the issue is that the song gets 'stuck'. This must have to do with the fact that is is in draw.

Looking forward to your replies!

Best wishes

import ddf.minim.*;

Minim minim;
AudioPlayer player;

boolean rect3Over = false;
boolean checked3 = false;
// Diameter of rectangles
int rectSize = 90;
// Position of play rect
int rect3X = 640/2 - rectSize/2;
int rect3Y = 250;

// Initialize width
int width = 640;

void setup()
{
  size(width, 360);

  minim = new Minim(this);

  player = minim.loadFile("1.mp3");
}

void draw() {
  background(255);

  // Draw play rect
  fill(175);
  rect(rect3X, rect3Y, rectSize, rectSize);

  // Test if the cursor is over play rect
  if (mouseX > rect3X && mouseX < (rect3X + rectSize) &&
    mouseY >rect3Y && mouseY < (rect3X + rectSize)) {
    rect3Over = true;
  } else {
    rect3Over = false;
  }

  //play song
  if (checked3) {
    player.play();
  }
}

// Check whether play rect is clicked
void mousePressed() {
  if (rect3Over) {
    checked3 = !checked3;
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles