# Using Minim to Play Sounds in States

**URL:** <https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639>\
**Category:** Libraries\
**Created:** [April 25, 2019, 7:40pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639 "2019-04-25T19:40:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danikc8](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@danikc8](https://discourse.processing.org/u/danikc8)\
**Post date:** [April 25, 2019, 7:40pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/1 "2019-04-25T19:40:28Z")

</div>

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:

```auto
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();
  }
    
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 29, 2019, 11:06pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/2 "2019-04-29T23:06:12Z")

</div>

> [@danikc8](#):
>
> I want the player1 clip to start looping in the stateMainScreen but I can’t figure out how to get it to do that.

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

- [Minim : : AudioPlayer](http://code.compartmental.net/minim/audioplayer_class_audioplayer.html)
- [Minim : : AudioPlayer : : loop](http://code.compartmental.net/minim/audioplayer_method_loop.html)

---

<div class="post-metadata">

**Author:** ![danikc8](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@danikc8](https://discourse.processing.org/u/danikc8)\
**Post date:** [May 2, 2019, 3:06pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/3 "2019-05-02T15:06:17Z")

</div>

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. 😕

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 2, 2019, 3:58pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/4 "2019-05-02T15:58:20Z")

</div>

> [@danikc8](#):
>
> how to pause it when an area on the screen is clicked and for another clip to loop instead.

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

> [@danikc8](#):
>
> player2 will play. Is there a way I can get it to not play until you are already on the stateMainScreen?

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

```auto
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 

```

---

<div class="post-metadata">

**Author:** ![danikc8](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@danikc8](https://discourse.processing.org/u/danikc8)\
**Post date:** [May 2, 2019, 4:25pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/5 "2019-05-02T16:25:06Z")

</div>

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. 😕

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 2, 2019, 4:49pm UTC](https://discourse.processing.org/t/using-minim-to-play-sounds-in-states/10639/6 "2019-05-02T16:49:27Z")

</div>

[http://code.compartmental.net/minim/audioplayer\_method\_pause.html](http://code.compartmental.net/minim/audioplayer_method_pause.html)

can you show your entire code please?

Does `play` work?
