Dynamic ofSoundPlayer

I am attempting to dynamically load in the contents of a directory of sounds, assign them with ofSoundPlayer and have them triggered by a keypress.

I am using the dirlist example and am running into an issue where no matter what part of the vector i refernce, it plays the sound that was last in the directory. It seems that all references in the vector as being assigned the last file in the directory.

The following is my code. Keypress 0 and 1 both play the same sound file, of which is whatever is the last sound file in the directory:

  
 dir.listDir("sounds/");  
	dir.sort();   
	  
//allocate the vector to have as many ofSoundPlayer as files  
if( dir.size() ){  
		sounds.assign(dir.size(), ofSoundPlayer());  
  
			}  
  
for(int i = 0; i < (int)dir.size(); i++){  
	sounds[i].loadSound(dir.getPath(i));  
  
  
 if (key == '0')  
    {  
        sounds[0].play();  
    }  
  
    if (key == '1')  
    {  
        sounds[1].play();  
    }  
  

This is a bit trickier because the sound player is actually just a wrapper for a subsystem that actually handles all of the loading and unloading sounds. What you want to do is just load and play the sound on key press.

  
  
void testApp::keyPressed(int key){  
  
    if(sound.isLoaded())  
        sound.unloadSound();  
      
    if(key == '0') {  
        sound.loadSound(dir.getPath(0));  
        sound.play();  
    }  
    if(key == '1') {  
        sound.loadSound(dir.getPath(1));  
        sound.play();  
    }  
    if(key == '2') {  
        sound.loadSound(dir.getPath(2));  
        sound.play();  
    }  
}  
  

Umm, yeah, now i know why i posted in beginners section… (smiling sheepishly)… thanks for the help.