Playing sounds at different times

Hi, I’m fairly new to openFrameworks, and I’m having an issue with playing different sounds in my program. I have three different sounds I want to play when different triggers are activated. I have set up a variable called ‘musicNum’ that initializes at 0 and increments when certain events happen.

if (musicNum == 0) {
	bgMusic[0].play();
	bgMusic[1].stop();
	bgMusic[2].stop();
}

else if (musicNum == 1) {
	bgMusic[1].play();
	bgMusic[0].stop();
	bgMusic[2].stop();
}

else if (musicNum == 2) {
	bgMusic[2].play();
	bgMusic[0].stop();
	bgMusic[1].stop();
}

This is my code that I have been trying. Right now, the when musicNum is equal to 0 the first song plays, but then when musicNum is equal to one, the first song stops, but the second song does not play.

If anyone has any suggestions please let me know!

are the samples loading ok? do you get any error messages?

class ofApp : public ofBaseApp{

	public:
		void setup();

		void keyPressed(int key);

    int musicNum;
    vector<ofSoundPlayer> bgMusic;
};
//--------------------------------------------------------------
void ofApp::setup(){
    musicNum =0;
    int maxSnds = 3;
    for (int i=1; i<=maxSnds; i++){
        ofSoundPlayer newSound;
        newSound.load("sample" + ofToString(i) + ".aif");
        bgMusic.push_back(newSound);
    }   
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    
    switch (key) {
        case '1':
            musicNum =0;
            break;
        case '2':
            musicNum =1;
            break;
        case '3':
            musicNum =2;
            break;
            
        case ' ':
            if (musicNum == 0) {
                bgMusic[0].play();
                bgMusic[1].stop();
                bgMusic[2].stop();
            } else if (musicNum == 1) {
                bgMusic[1].play();
                bgMusic[0].stop();
                bgMusic[2].stop();
            } else if (musicNum == 2) {
                bgMusic[2].play();
                bgMusic[0].stop();
                bgMusic[1].stop();
            }
            break;
        default:
            break;
    }
}

this works fine - samples are ‘sample1.aif’ etc…

Hi, thank you for your reply. I was able to work around the issue by just adjusting the volumes, so that the track I wanted was at full volume, and the others were silent!