I’m trying to make a music player using minim and I have a progress bar thats halfway down the screen, the way I intend it to work is when you click on the progress bar the song will skip to that part, I know something is wrong with my mapping but I don’t know where to fix it. Here is my code:
import ddf.minim.*;
Minim minim;
AudioPlayer player;
AudioMetaData meta;
int TimeStamp = 45;
void setup() {
size(504, 204);
textSize(25);
textAlign(CORNER);
minim = new Minim(this);
player = minim.loadFile("Song.mp3");
meta = player.getMetaData();
player.loop();
}
void draw() {
stroke(0);
background(255, 255, 255, 255);
fill(255);
strokeWeight(1);
rect(2, 2, 500, 200, 30);
rect(45, 45, 80, 80);
fill(0);
text(meta.title(), 135, 70);
text(meta.author(), 135, 110);
strokeWeight(20);
stroke(204);
line(45, 150, width - 45, 150); //where the progress bar should end
stroke(0);
//here is where it goes wrong
TimeStamp = int( map(player.position(), 0, player.length(), 45, width - 45));
line(45, 150, TimeStamp, 150);
}
int position;
void mouseReleased() {
if (mouseX > 45 && mouseX < width - 45) {
position = int( map( mouseX, 45, width - 45, 0, player.length() ) );
player.cue( position );
println("true");
} else {
println("false");
}
}
basically the song finishes before the bar gets to the end
its width - 45 because thats the X position of the end of the gray bar and where I want the progress bar to end, setting it to width - 90 (in both and the first map) makes it end farther away from the end of the gray bar
I looked at your code and couldn’t find anything wrong, so I tested your code with an mp3 of mine, and it worked flawlessly. Maybe the problem is with Song.mp3. Have you tried a different mp3?
I just tried it with 3 other songs and its the same problem…
although some songs did get closer to the end than others (just by eye)
I really have no clue what could be going on with that…
Am I allowed to upload the song that I tried with so you could get the exact same results?
Yes You are very correct. There is a mismatch between song’s length and the final position, which I found using a couple of println statements. You’re a keen observer. But the cause of this bug I don’t know and have to do some research on my part.
But it worked for the other song I checked with. Perhaps this is an artifact of mp3 songs.
Thanks for your help!
Its good to see that someone else can also see this, I was thinking of just reinstalling processing but now i dont think that would do anything.
Could you show me where you put the println statements so I can also do some research
Thanks for your help!
I can see that the song position is different to the song length when the song is finished and your code
I also like the redesign with the red rectangle
Although I have found that sometimes when you click to skip to a part of the song the bar doesn’t go to exactly where you clicked (EDIT: to recreate, when the song is finished click around 20-40 pixels left of the edge of the progress bar) but I think thats just because the way that the line is displayed but then it doesn’t jump when the song is finished because then it works as intended. I think I can fix that.
Thanks again for your help!
Thank You For Your Appreciation. I’ve forgotten to replace song.length() with songLength variable in mouseReleased() method as follows.
. Kindly do it then there will not be any glitches.
void mouseReleased() {
int position;
if (mouseX > SPACING && mouseX < width - SPACING) {
position = int(map( mouseX, SPACING, width - SPACING, 0, songLength));
song.play();
song.cue(position);
}
}
Also you can try writing a function you can use in the setup() itself to pre-calculate the real length of a song.