How do I play sound files using the ControlP5 library import

89 Views Asked by At

I'm trying to play a sound file by clicking on a button in processing using Controlp5 and processing.sound. I'm having trouble finding a way to declare when I press on the button, a sound file plays. This is all that I have so far. I don't have much written simply because I don't know what to write.

import controlP5.*;

import processing.sound.*;
SoundFile song;

ControlP5 cp5;

boolean playmusic = false;

void setup() {
  size(800, 800);
  song = new SoundFile(this, "my-sound-file.mp3");

  cp5 = new ControlP5(this);

  cp5.addButton("play")
    .setValue(0)
    .setPosition(300, 100)
    .setSize(200, 100)
    ;
}

void draw(){

}

This is my first time posting so if I should be formatting this question differently, please tell me.

1

There are 1 best solutions below

0
apodidae On BEST ANSWER

You have to create a separate function called 'play' which will run when the button is pressed. Place all of the sound code in that function. The xxx.mp3 file should ideally be placed in a folder entitled 'data' inside of your sketch folder. Processing knows to look there for it.

import controlP5.*;
import processing.sound.*;

SoundFile song;
ControlP5 cp5;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);
  cp5.addButton("play")
    .setPosition(100, 100)
    .setSize(200, 100)
    ;
}

public void play() { 
  song = new SoundFile(this, "mySnd.mp3");
  song.play();
}

void draw(){
}