[SOLVED] Minim progress bar

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

Width-90 because 90=2x45

That doesn’t work for me…

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

The last entry is width-90

yea… thats what I tryed

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?

Instead of uploading the song, post a link to download the song.

Your little program worked for me and nothing to complaint…

https://drive.google.com/file/d/1l4X5sFzYo2H_PxYNbbKKfr2sVKaohWbF/view?usp=sharing

thats the song I used and it stops around like 20 pixels before its supposed to

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

Interesting

Maybe the song has just silence at the end

:wink:

No It’s not playing …

I’ve patched up a temporary solution. Did not need a patch up for the third song. It played flawlessly till the end.


import ddf.minim.*;

Minim minim;
AudioPlayer song;
AudioOutput out;
AudioMetaData meta;

final float SPACING = 50.0f;
final boolean DEBUG_ON = false;

float TimeStamp = SPACING;

float songLength;

void setup() {
  size(500, 200);
  textSize(14);
  textAlign(CORNER);
  frameRate(240);

  minim = new Minim(this);
  out = minim.getLineOut(Minim.STEREO, 1024);

  //song = minim.loadFile("Kaadhal Kasakudhaiya.mp3");
  song = minim.loadFile("Song.mp3");
  // song = minim.loadFile("Agni Natchatiram - Ninnukori Varanam.mp3");
  //song = minim.loadFile("Bhavayami.mp3");

  meta = song.getMetaData();
  song.play();
  songLength = song.length();
}

void draw() {
  stroke(0);
  background(51, 50);
  fill(100, 50, 50); 
  strokeWeight(0);
  strokeCap(SQUARE);
  rect(20, 20, width-40, height-40, 40);
  fill(100); 
  rect(SPACING, SPACING, 80, 80);
  fill(255);
  text(meta.title(), 135, 70, 300, 50);
  text(meta.author(), 135, 110, 300, 50);

  strokeWeight(20);
  stroke(204);
  line(SPACING, 150, width - SPACING, 150); //where the progress bar should end
  stroke(0, 0, 255, 128);

  //here is where it goes wrong. <KAZ> Now I've padched up it a little to make things work better
  TimeStamp =  map(song.position(), 0, songLength, 0, width - 2*SPACING);
  if (DEBUG_ON) {
    println("Progress Bar Position: " + (SPACING + TimeStamp));
    println("Song Position: " + song.position());
    println("Song Length: " + song.length());
    println(song.isPlaying());
  }
  if (!song.isPlaying()) {
    songLength = song.position();
  }
  line(SPACING, 150, SPACING+TimeStamp, 150);
}


void mouseReleased() {
  int position;
  if (mouseX > SPACING && mouseX < width - SPACING) {
    position = int(map( mouseX, SPACING, width - SPACING, 0, song.length()));
    song.play();
    song.cue(position);
  }
}

1 Like

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 :stuck_out_tongue:

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!


Doge

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.

1 Like

Yea the setup function sounds like a good idea! Ill try that out!

Edit: This fixed it!

void InitSong() {
  song.cue(song.length());
  songLength = song.position();
  song.cue(0);
}

I also removed the songLength = song.length();in setup and it just works now!

Thanks for all your help again!


Doge

2 Likes