I’m trying to make a song player with three buttons (go to prev song, stop song, go to next song). I want to stop the audio playing when the stop button button is clicked using mousePressed().
The problem is, it is applied whenever my mouse is hovering on the area of the button I defined in the mousePressed() function. Why is that so? I tried to use other area as well inside the mousePressed() and still facing the same problem.
Here is my code:
import processing.serial.*;
import processing.sound.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import cc.arduino.*;
Arduino arduino;
Serial myPort;
Minim minim;
AudioPlayer song;
AudioMetaData meta;
Amplitude amp;
AudioIn in;
BeatDetect beat;
BeatListener bl;
SoundFile now_playing;
Waveform waveform;
float DEFAULT_VOL_AMP = 1;
PFont poppins;
PImage prevIcon, playIcon, nextIcon;
boolean prevPressed, playPressed, nextPressed = false;
// ----- songs & waveform -----
String songs[] = {"song1.mp3",
"song2.mp3",
"song3.mp3",
"song4.mp3",
"song5.mp3",
"song6.mp3"};
int songsLength = songs.length;
int pointer = 2;
String decibelsLevel = "";
int samples = 60000; // detail waveform
boolean displayWarning = false;
String warning = "WARNING! n Your song is too loud, it can damage your hearing.";
int prevX, prevY, playX, playY, nextX, nextY, iPrevX, iPrevY, iPlayX, iPlayY, iNextX, iNextY; // posisi x & y dari masing-masing button
int btnSize = 72;
color btnColor, prevBtnColor, playBtnColor, nextBtnColor;
public void setup() {
size(800, 500);
poppins = createFont("poppins-regular.ttf", 22);
textFont(poppins);
// waveform
now_playing = new SoundFile(this, songs[pointer]);
now_playing.loop();
waveform = new Waveform(this, samples);
waveform.input(now_playing);
now_playing.amp(DEFAULT_VOL_AMP);
// scans amplitude level
amp = new Amplitude(this); in = new AudioIn(this, 0);
in.start();
amp.input(now_playing);
// getting song's metadata
minim = new Minim(this);
song = minim.loadFile(songs[pointer]);
meta = song.getMetaData();
// buttons & icons
btnColor = color(85,103,186);
prevBtnColor = color(85,103,186);
prevX = 300; iPrevX = prevX - 25;
prevY = 400; iPrevY = prevY - 25;
prevIcon = loadImage("icon_previous.png");
playBtnColor = color(85,103,186);
playX = 400; iPlayX = playX - 15;
playY = 400; iPlayY = playY - 18;
playIcon = loadImage("icon_play.png");
nextBtnColor = color(85,103,186);
nextX = 500; iNextX = nextX - 25;
nextY = 400; iNextY = nextY - 25;
nextIcon = loadImage("icon_next.png");
}
public void draw() {
background(49, 65, 161); // blue
AmplitudeLevel();
Waveform();
if (amp.analyze() > 2){
DisplayWarning();
}
else {
SongDesc();
}
Buttons();
mousePressed();
}
public void mousePressed(){
// prevBtn position
if (mouseX > prevX-btnSize && mouseX < prevX+btnSize && mouseY > prevY-btnSize && mouseY < prevY+btnSize){
if (songs[pointer-1] != null) {
pointer--;
}
else {
pointer = songsLength - 1;
}
}
// stopBtn position
else if (mouseX > playX-btnSize && mouseX < playX+btnSize && mouseY > playY-btnSize && mouseY < playY+btnSize){
now_playing.stop();
}
// nextBtn position
else if (mouseX > nextX-btnSize && mouseX < nextX+btnSize && mouseY > nextY-btnSize && mouseY < nextY+btnSize){
if (songs[pointer + 1] != null) {
pointer++;
}
else {
pointer = 0;
}
}
}
I also tried to debug with the code below. However, the “pressed” printed even though I didn’t create any mouse event.
boolean alreadyPressed = false
public void mousePressed(){
if (!alreadyPressed) {
println("pressed");
alreadyPressed = true;
}
}
Thanks in advance & I’ll really appreciate any help.
Advertisement
Answer
It is because you are calling mousePressed inside draw. The function mousePressed itself doesn’t check if the mouse is actually pressed. It is just a function that is called every time the mouse is pressed. The checking part is done elsewhere in Processing.
Now that you are calling it in draw, every time you draw, it executes mousePressed function and makes everything that is supposed to be executed on press execute on hover.