Use of If and Else conditional to make sound pause and play.

edited December 2017 in Library Questions

Hello! I am almost complete with my project. I have decided to use the keyboard function to have a certain key play a sound. I have gotten as far as that. But, I am stuck at how to pause the sound and also play for the same key. For Example: if you press a letter once, it'll play, and if you do it again, it'll pause it and so forth.

I've tried to use filename.rewind(); but that doesn't help from all my sounds playing at once.

import ddf.minim.*;
import processing.sound.*;

Minim minim;
AudioPlayer winter;
AudioPlayer summer;
AudioPlayer spring;
AudioPlayer fall;


MyImage flower;
MyImage fire;
MyImage sun;
MyImage snowman;
float xpos, ypos;    // Starting position of shape    

float xspeed = 5;  // Speed of the shape
float yspeed = 15;  // Speed of the shape

int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom

color c;

void setup() {
  size(800, 800);
  c = color(random(255), random(255), random(255));
  noStroke();


  xpos = width/1.8;
  ypos = height/3;
  flower=new MyImage("Flower.png");
  fire=new MyImage("Sun.png");
  sun=new MyImage("campfire.png");
  snowman=new MyImage("snowman.png");

  // will play sound
  minim = new Minim(this);
  winter = minim.loadFile("Winter Mix.wav");
  summer = minim.loadFile("Summer Mix.wav");
  spring = minim.loadFile("Spring Mix.wav");
  fall = minim.loadFile("Fall Mix.wav");
}   


void draw() {
  background(c);
  textSize(20);
  text("R for Spring S for Summer & W for Winter", 80, 100);
  flower.update();
  sun.update();
  fire.update();
  snowman.update();

  flower.draw();
  sun.draw();
  fire.draw();
  snowman.draw();

  if ((keyPressed == true) && ((key == 'S') || (key =='s'))) {
    summer.play();
    summer.rewind();
  }
  if ((keyPressed == true) &&((key == 'W') || (key=='w'))) {
    winter.play();
  }
  if ((keyPressed == true) && ((key == 'R') || (key =='r'))) {
    spring.play();
  }
  if ((keyPressed == true) && ((key == 'F') || (key =='f'))) {
    fall.play();
  }
}
void mousePressed() {
  c = color(random(255), random(255), random(255));
}

class MyImage {  // A class that specifically keeps the images within the canvas. Helped with Forum in Processing 

  PImage img;
  float x, y;
  int w, h;
  float xspeed, yspeed;


  MyImage(String flower) {
    w=h=200;   
    x=random(w, width-w);
    y=random(h, height-h);
    xspeed=yspeed=5;
    img=loadImage(flower);
    img.resize(200, 200);
  }

  MyImage() {
    w=h=200;   
    x=random(w, width-w);
    y=random(h, height-h);
    xspeed=yspeed=5;
    img=getMeImage(color(0));
  }

  MyImage(color c) {
    w=h=200;   
    x=random(w, width-w);
    y=random(h, height-h);
    xspeed=random(9, 13);    
    yspeed=random(7, 10);
    img=getMeImage(c);
  }


  PImage getMeImage(color c) {

    PImage img=createImage(w, h, RGB);
    img.loadPixels();
    for (int i=0; i<200*200; i++)
      img.pixels[i]=c;
    img.updatePixels();
    return img;
  }

  void update() {
    if (x > width-w || x < 0) {
      xspeed *= -1;
    }
    if (y > height-h || y < 0) {
      yspeed *= -1;
    }
    x+=xspeed;
    y+=yspeed;
  }

  void draw() {
    image(img, x, y);  //ImageMode corners
  }
}

This is the full code. Thank you for your help

Melanie

Tagged:

Answers

  • It is better if you post all your code. You can edit the post above and add the code there, if you wish.

    Related to your question. So a single key toggles a single sound? Would you be playing multiple sounds together? For example if I pressed S then W then R almost together?

    Kf

  • Hi Kfrager! You're correct, if you press all the specified Keys, then all the sounds will play at once. I don't mind having that as an option, I would just like to know how to make them turn off or pause.

  • Also, My fall sound does not play for some reason, and if you press the designated F key, it'll stop the program completely. The note says

     === Minim Error ===
    === Couldn't load the file Fall Mix.wav 
    

    I don't know whats wrong with that file cause, it should be the same as others.

    But thank you again!

  • Check the spelling of the filename. Double check. You can also create a small sketch loading that specific file and trying it out by itself. Or see if you can get the file from another source or use an alternative, at least for testing. It is not unheard of for a file to be corrupt.

    Check these posts as it might help in your quest:

    https://forum.processing.org/two/discussion/comment/90737/#Comment_90737
    https://forum.processing.org/two/discussion/21316/how-do-i-play-the-same-soundfile-multiple-times-simultaneously#latest

    Kf

Sign In or Register to comment.