0

I've been trying to figure this out for ages. The Stop() function doesn't to work in the if statement (the songs just play on top of each other rather than pausing and rewinding). If I place the Stop() function call outside the if statement, it seems like it works (ie no file is played at all which is expected behavior) Wondering if anyone could shed some light?

--------------SAMPLE OF CODE -------------------------

      import processing.serial.*;
import ddf.minim.*;
String id;
Card Card1 = new Card();
Minim minim;//audio context;
AudioPlayer Backgroundplayer;

   public class Card{ // CARD OBJECT so can set up each card.
     String id;
     String FileToPlay;
     boolean Playing = false;
     AudioPlayer player;
     public Card(){}
     public  Card(String ID, String FILETOPLAY){
       id = ID;
       FileToPlay = FILETOPLAY;
     }
     void Load(){
       player = minim.loadFile(FileToPlay);
     }
     void Play(){
        if(Playing == false){
           player.play();
           Playing = true;
           println("PLaying");
          }else if(Playing == true){
            Playing = false;
            Stop();
        }

   }

        void Stop(){
         println("Stopping");
         player.pause();
          player.rewind();
       }

   }

void setup() {
  size(400, 300);
}

void draw() {

}

void mouseClicked() {
  Card1.id = id;
  Card1.FileToPlay = "drumploop.wav";
  Card1.Load();
  Card1.Play();
}

The desired outcome is to play different sounds with each card. However, when a card is scanned, and if the card is already playing a clip, when its scanned again it should stop that assigned clip from playing, whilst the remaining other clips still play.

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
  • When do you think the `else` part of your if statement will ever be entered? – Kevin Workman Jan 10 '16 at 22:36
  • The else part will be entered when the bool playing is true, i.e when the sound has been played so the next time i play that sound, it would then go into the else, that's what i think anyway – FARO PHIRI Jan 11 '16 at 18:06

1 Answers1

0

You seem to be under the impression that the if/else statement in your Play() function will be triggered twice: once when you start to play the clip, and once when you play another clip.

However, that's not the case: you only ever seem to call the Play() function once for each instance of Card. In other words, even though you might be creating multiple instances of Card and calling the Play() function on each one, that's still only once per instance! Let's make it more obvious:

Card card1 = new Card("one", "clip1.wav");
card1.Load(); //load the first clip
card1.Play(); //play the first clip

Card card2 = new Card("two", "clip2.wav");
card2.Load(); //load the second clip
card2.Play(); //play the second clip

Notice that when you call card2.Play(), nothing is calling card1.Play() or card1.Stop(), so the first clip continues playing!

You'll have to specifically call the Stop() function of any playing clips if you want them to stop before playing a new one:

Card card1 = new Card("one", "clip1.wav");
card1.Load(); //load the first clip
card1.Play(); //play the first clip

Card card2 = new Card("two", "clip2.wav");
card1.Stop(); //stop the first clip
card2.Load(); //load the second clip
card2.Play(); //play the second clip
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Sorry my question should have been clearer, the desired outcome is to play different sounds with each card. However, when a card is scanned, and if the card is already playing a clip, when its scanned again it should stop that assigned clip from playing, whilst the remaining other clips still play – FARO PHIRI Jan 11 '16 at 18:18
  • How are you scanning the cards? Can you post an [MCVE](http://stackoverflow.com/help/mcve)? – Kevin Workman Jan 11 '16 at 18:21
  • Hi. I'm scanning the cards with an RFID reader, so when each card is scanned there is an if statement to find out which card has been scanned and when it has it plays the appropriate clip – FARO PHIRI Jan 11 '16 at 18:23
  • We'd still need to see how you're determining which existing Card instance should be played or stopped. Consider creating a small example that uses a mouse click instead of an RFID scanner. – Kevin Workman Jan 11 '16 at 18:24
  • Hi sorry, ive just posted a sample of code, however i read this too late. Im not very familiar with processing and im just helping a friend out with some logic, i just needed an idea of why the stop wasn't working, i hope i have given enough information – FARO PHIRI Jan 11 '16 at 18:30
  • The problem is that you've only posted the initialization. We need to see how you're fetching the already playing Card instances. The best way to do that would be to put together a small working example that we could run. – Kevin Workman Jan 11 '16 at 18:31
  • Hi, i've Edited the question and tried to provide you with a sample, however you will need to provide the sound file. i tried, sorry if ive done something wrong – FARO PHIRI Jan 11 '16 at 18:41