Music player now rewinds the whole song

I’m new to Processing and programming overall. I’ve been making this code as part of my mid-term assigment. It consists on making a music player with the help of the Minim library. I cannot write code I haven’t been tought such as “new”, although I did use it once for Minim at the beginning of my sketch, so I cannot really use it.

My biggest concern right now is that I cannot make the music to pause and play from where it stopped, it just rewinds everything. Don’t really know what I did to it, but it was working just 10 minutes ago.

Sorry if I seem like a newbie. Anyways, here is the code:

import ddf.minim.*;

Minim minim;
AudioPlayer jazz1;
//AudioPlayer jazz2;
//AudioPlayer jazz3;

float x;

void setup(){
  // setup
  noStroke();
  size(600,800);
  
  minim = new Minim(this);
  
  jazz1 = minim.loadFile("Overture.mp3");
  //jazz2 = minim.loadFile("Too Hip To Retire.mp3");
  //jazz3 = minim.loadFile("Whiplash.mp3");  
  
  jazz1.play();
}

void draw(){
  background(#2F2D2E);
    
  // Bar
  fill(#792359);
  rect(0,600,600,200);
  
  // Play/Pause Circle
  strokeWeight(1);
  stroke(#FD3E81);
  circle(width/2,700,100);
  noStroke();
  
  // Play/Pause Cycle
  if (jazz1.isPlaying()==true){
    fill(#FD3E81);
    rect(width/2-20,675,10,50);
    rect(width/2+10,675,10,50);
  } else {
    // Pause Vector
    fill(#FD3E81);
    triangle(width/2-15,675,width/2+25,700,width/2-15,725);
  }
  
  // Seek Backwards Vector
  fill(#FD3E81);
  rect(width/2-145,675,10,50);
  triangle(width/2-130,700, width/2-90,675, width/2-90,725);
  
  // Seek Forward Vector
  fill(#FD3E81);
  rect(width/2+135,675,10,50);
  triangle(width/2+130,700, width/2+90,675, width/2+90,725);
  
  // Duration Info
  x = map(jazz1.position(),0,jazz1.length(),0,width);
  strokeWeight(2);
  stroke(#FD3E81);
  line(0,600,x,600);
  noStroke();
 
}

void mouseReleased(){
  // mousePressed for Play Button
  if (hoverPlay()==true && jazz1.isPlaying() ){
    jazz1.pause();
  } else if (hoverPlay()==true && jazz1.position() == jazz1.length() ){
    jazz1.rewind();
    jazz1.play();
  } else if (hoverPlay()==true ) {
    jazz1.play();
  }
  
  // mousePressed for Backwards Button
  if (hoverBack()==true && jazz1.position() < 3000) {
    jazz1.pause();
    jazz1 = minim.loadFile("Too Hip to Retire.mp3");
    jazz1.play();
  } else {
    jazz1.rewind();
  }
}


// Boolean for Play/Pause Button
boolean hoverPlay() {
 if (mouseX >= width/2-50 && mouseX <= width/2+50 && mouseY >= 650 && mouseY <= 750){
   return true;
 } else {
   return false;
 }
}
 
boolean hoverBack() {
  if (mouseX >= width/2-145 && mouseX <= width/2-90 && mouseY >= 650 && mouseY <= 750){
    return true;
  } else {
    return false;
 }
}

boolean hoverForward() {
  if (mouseX >= width/2+90 && mouseX <= width/2+145 && mouseY >= 650 && mouseY <= 750){
    return true;
  } else {
    return false;
  }
}
1 Like
1 Like

Thank you for your response. I ended up learning how to make objects and arrays all day.

I know my code looks still like something someone new would do, but I’m still proud of it.

My code ended up looking like this:

// make minim library usable
import ddf.minim.*;

// minim needed code
Minim minim;

// define class and array
AudioPlayer[] jazz = new AudioPlayer[5];

// needed variable for whole program
int i;

void setup(){  
  // setup
  noStroke();
  size(600,800);
  
  minim = new Minim(this);
  
  // define songs for array 
  jazz[0] = minim.loadFile("Overture.mp3");
  jazz[1] = minim.loadFile("Too Hip to Retire.mp3");
  jazz[2] = minim.loadFile("Whiplash.mp3");
  jazz[3] = minim.loadFile("Practicing.mp3");
  jazz[4] = minim.loadFile("Caravan.mp3");
  
  // define which song stars
  i=1;
  
  // star program playing
  jazz[i].play();
}

void draw(){
  background(#2F2D2E);
    
  // Bar
  fill(#792359);
  rect(0,600,600,200);
  
  // Play/Pause Circle
  strokeWeight(2);
  stroke(#FD3E81);
  circle(width/2,700,100);
  noStroke();
  
  // Play/Pause Cycle
  if (jazz[i].isPlaying()==true){
    fill(#FD3E81);
    rect(width/2-20,675,10,50);
    rect(width/2+10,675,10,50);
  } else {
    // Pause Vector
    fill(#FD3E81);
    triangle(width/2-15,675,width/2+25,700,width/2-15,725);
  }
  
  // Seek Backwards Vector
  fill(#FD3E81);
  rect(width/2-145,675,10,50);
  triangle(width/2-130,700, width/2-90,675, width/2-90,725);
  
  // Seek Forward Vector
  fill(#FD3E81);
  rect(width/2+135,675,10,50);
  triangle(width/2+130,700, width/2+90,675, width/2+90,725);
  
  // Duration Info
  float x = map(jazz[i].position(),0,jazz[i].length(),0,width);
  strokeWeight(2);
  stroke(#FD3E81);
  line(0,600,x,600);
  noStroke();
 
}

void mouseReleased(){
  // mousePressed for Play Button
  if (hoverPlay()==true && jazz[i].isPlaying() ){
    jazz[i].pause();
    
  } else if (hoverPlay()==true && jazz[i].position() == jazz[i].length() ){
    jazz[i].rewind();
    jazz[i].play();
    
  } else if (hoverPlay()==true ) {
    jazz[i].play();
  }
  
  // mousePressed for Backwards Button
  if (hoverBack()==true && jazz[i].position() < 3000 && i==0) {
    jazz[i].pause();
    i=jazz.length-1;
    jazz[i].rewind();
    jazz[i].play();
    
  } else if (hoverBack()==true && jazz[i].position() < 3000) {
    jazz[i].pause();
    i--;
    jazz[i].rewind();
    jazz[i].play();
    
  } else if (hoverBack()==true) {
    jazz[i].rewind();
  }
  
  // mousePressed for Forward Button
  if (hoverForward()==true && i==jazz.length-1){
    jazz[i].pause();
    i=0;
    jazz[i].rewind();
    jazz[i].play();
    
  } else if (hoverForward()==true){
    jazz[i].pause();
    i++;
    jazz[i].rewind();
    jazz[i].play();
  }
}


// Boolean for Play/Pause Button
boolean hoverPlay() {
 if (mouseX >= width/2-50 && mouseX <= width/2+50 && mouseY >= 650 && mouseY <= 750){
   return true;
 } else {
   return false;
 }
}

 // Boolean for Backwards Button
boolean hoverBack() {
  if (mouseX >= width/2-145 && mouseX <= width/2-90 && mouseY >= 650 && mouseY <= 750){
    return true;
  } else {
    return false;
 }
}
// Boolean for Forward Button
boolean hoverForward() {
  if (mouseX >= width/2+90 && mouseX <= width/2+145 && mouseY >= 650 && mouseY <= 750){
    return true;
  } else {
    return false;
  }
}
1 Like

A util function to pause() & rewind() all AudioPlayer instances would simplify your code by a lot: :saxophone:

void pauseRewindAll() {
  for (final AudioPlayer j : jazz) {
    j.pause();
    j.rewind();
  }
}
1 Like