Using Minim to Play Sounds in States

Hi there! This is my first Processing project and I’m having some difficulty with the sounds playing when I want them to. I want the player1 clip to start looping in the stateMainScreen but I can’t figure out how to get it to do that.

My second problem is that I only want the player2 sound to play in the stateMainScreen when the circle is clicked. But on the stateStartScreen, if you click in the top left corner of the “enter” button (where the circle will be on the next screen), player2 will play. Is there a way I can get it to not play until you are already on the stateMainScreen?

Sorry if this post is confusing. Any help is appreciated!

Here is my code so far:

import ddf.minim.*;
Minim minim;
AudioPlayer player1;
AudioPlayer player2;
AudioPlayer player3;
AudioInput input;

final int stateStartScreen = 0;
final int stateMainScreen = 1;
int state = stateStartScreen;

PImage img;
PFont font;

Button my_button;
int clk = 1;

Circle circle1;

void setup() {
  size(800, 600);
  my_button = new Button("Enter", 320, 320, 100, 50);
  circle1 = new Circle(320, 320, 40, 40);

  //String[] fontlist = PFont.list();
  //printArray(fontlist);

  font = createFont("Geneva", 14);
  textFont(font);
  img = loadImage("Start-Screen-Background.jpg");

  minim = new Minim(this);
  player1 = minim.loadFile("Cicadas.mp3");
  player2 = minim.loadFile("Rain.mp3");
  player3 = minim.loadFile("Starting-burner.mp3");
}

void draw() {
  switch(state){
    case stateStartScreen:
      showStartScreen();
      break;
    case stateMainScreen:
      showMainScreen();
      break;
  }
}

void showStartScreen(){
  background(img);
  my_button.Draw();
}

void showMainScreen(){
  background(34, 139, 34);
  //To see x & y coordinates on the screen (remove later)
  textSize(10);
  text("x:" + mouseX + "y:" + mouseY, 30, 10);
  textSize(32);
  text("Main", 255, 255, 255);
  circle1.Draw();
  //sound isn't playing
  player1.loop();
}

class Circle {
  float x;
  float y;
  float w;
  float h;
  
  Circle(float xpos, float ypos, float widthA, float heightA) {
    x = xpos;
    y = ypos;
    w = widthA;
    h = heightA;
  }
  
  void Draw() {
    fill(0);
    stroke(0);
    ellipse(x, y, w, h);
  }
  
  boolean overCircle() {
  if (mouseX >= 305 && mouseX < (305 + 40) &&
      mouseY >= 305 && mouseX < (305 + 40)) {
        return true;
      } else {
        return false;
      }
  }
}
    

class Button {
  String label;
  float x;
  float y;
  float w;
  float h;
  
  Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
    label = labelB;
    x = xpos;
    y = ypos;
    w = widthB;
    h = heightB;
  }
  
  void Draw() {
    fill(218);
    stroke(141);
    rect(x, y, w, h, 10);
    textAlign(CENTER, CENTER);
    fill(0);
    text(label, x + (w/2), y + (h/2));
  }
  
  boolean overButton() {
  if (mouseX >= x && mouseX < (x + w) &&
      mouseY >= y && mouseY < (y + h)) {
        return true;
      } else {
        return false;
      }
  }
}

void mousePressed(){
  if(my_button.overButton()){
    state = stateMainScreen;
  }
}

void mouseClicked(){
  if(state == stateMainScreen && circle1.overCircle() == true) {
    player3.rewind();
    player3.play();
  }
    
}

Were you able to resolve this problem? Did you look at the AudioPlayer documentation – in particular, loop?

Thanks for the links! I got the clip to play on the main screen, but now I can’t figure out how to pause it when an area on the screen is clicked and for another clip to loop instead. And I’m still having the second issue. :confused:

Just use .pause(); on the first file and .loop on the other clip

in mousePressed() and mouseClicked() you should have the same switch clause that you have in draw:

  • Evaluate the mouse click depending on the state you are in.

Example:

void mousePressed(){
      
   switch(state){
      
    case stateStartScreen:
      if(my_button.overCircle()) {
        player1.rewind();
        player1.play();
      }
      break;
      
    case stateMainScreen:
      if(circle1.overCircle()) {
        player3.rewind();
        player3.play();
      }
      break;
        
  }//switch
     
}//func 

Thanks! the .pause and .loop are not working for some reason. I’ve tried putting them in a few different spots, but they never work for me. :confused:

void mousePressed(){
  if(my_button.overButton()){
    state = stateMainScreen;
    player1.loop();
  } else if (square2.overSquare()) {
    player1.pause();
    player2.loop();
  }
}

void mouseClicked(){
  switch(state) {
    case stateStartScreen:
      break;
    
    case stateMainScreen:
      if(square1.overSquare()) {
        player3.rewind();
        player3.play();
      } else if (square3.overSquare()) {
        player4.rewind();
        player4.play();
      } else if (square4.overSquare()) {
        player5.rewind();
        player5.play();
      } else if (square5.overSquare()) {
        player6.rewind();
        player6.play();
      } else if (square6.overSquare()) {
        player7.rewind();
        player7.play();
      }
      break;
  }
}

http://code.compartmental.net/minim/audioplayer_method_pause.html

can you show your entire code please?

Does play work?