Help with ofxDatGui dropdown Menu

hey just need a little help im building a dropdown list that will load music files
and they play when selected i will like them to stop playing when other files is selected in the same dropdown
so the code is like this
void ofApp::onDropdownEvent(ofxDatGuiDropdownEvent e){
if (e.target->getEnabled()==true){

        sounds[e.child].play();
        
         cout<<"numero"<<e.target->getIndex()<<endl;
         
     }

how can i make the selected sound to stop
i try this way but can’t make it work

if (e.target->getEnabled()==false) {
         sounds[e.child].stop();
     }

Hey, I’d like to understand it. You’ve got one dropdownlist with few sound files and is it possible to play all files at the same time? Maybe it’s better and easier to have only one sound player object and stop when it’s played and load and play when the new file is chosen.

yeah they all play at the same time if selected and i try just do one player but when it loads i get this

[ error ] ofFmodSoundPlayer: loadSound(): could not load “…/…/…/data/*102_I Never Dream.mp3”
when ever i choose a sound, the way i was do in it i get to play the sound but can make them stop if choose another one

How about something like this?

I have one ofSoundPlayer sound object and a std::vector<std::string> songs which hold all the filenames of the songs to play. Right now, I’m just doing something like this –

songs.push_back("sounds/song1.mp3");
songs.push_back("sounds/song2.mp3");
songs.push_back("sounds/song3.mp3");
songs.push_back("sounds/song4.mp3");

in ofApp::setup().

And then for the onDropdownEvent:

void ofApp::onDropdownEvent(ofxDatGuiDropdownEvent e)
{

	if ( sound.isPlaying() )
	{
		sound.stop();
		sound.load(songs[e.child]);
		if (sound.isLoaded() )
		{
			sound.play();
		}
	}
	else
	{
		sound.load(songs[e.child]);
		if ( sound.isLoaded() )
		{
			sound.play();
		}
	}
}

When I select a file from the dropdown menu it plays that file, and then when I select another file from the dropdown, it stops the currently playing one and plays the newly selected one.

Exactly. You can shorten your code a little bit because you always load a file:

void ofApp::onDropdownEvent(ofxDatGuiDropdownEvent e) {
    if (sound.isPlaying() ) {
         sound.stop();
    }

    sound.load(songs[e.child]);
        if (sound.isLoaded()) {
            sound.play();
	    }
}

mm great will try that but i was loading the song using ofDirectory like this

//    dir.listDir("audio");
//    dir.sort();
//    if( dir.size() ){
//        names.resize(dir.size());
//        
//        ofLog(OF_LOG_NOTICE,"songs loaded");
//    }
//    
//    
//    for ( int i =0; i<dir.size(); i++) {
//        songs[i].load(dir.getPath(i));
//        
//        fileInfo = "*" + ofToString(i) + dir.getName(i);
//        names.push_back(fileInfo);
//        
//    }

so i guess i will have to change that to something like this
//loading files
dir.listDir(“audio”);
dir.sort();
if( dir.size() ){
// sounds.resize(dir.size());
ofLog(OF_LOG_NOTICE,“songs loaded”);
}

for ( int i =0; i<dir.size(); i++) {
    //sounds[i].load(dir.getPath(i));
    
    fileInfo = "*" + ofToString(i) + dir.getName(i);
    names.push_back(fileInfo);
    
}

and it load fine but i still get this
error when i try to play a song
[ error ] ofFmodSoundPlayer: loadSound(): could not load “…/…/…/data/*102_I Never Dream.mp3”
strong text

never-mind i fixit

Thanks @ajbajb and @SebastianSobotka it was the way you told me that it finally work !!
thanks