Problem loading sound with ofSystemLoadDialog

Greetings, I’m trying to load an audio file with ofSystemLoadDialog and to play it with sound.load and sound.play
This is my code in ofApp.cpp:

ofSoundPlayer sound;

string FN;
string path;
ofFileDialogResult result = ofSystemLoadDialog(“Load file”);
if (result.bSuccess) {
string path = result.getPath();
string FN = ofFilePath::getFileName(path);
}

sound.load(FN);
sound.play();
sound.setLoop(true);

I’m trying to access the file by opening it with a dialog window, then assigning its name to FN string variable and then loading it with sound.load
I can’t find a way…Any help will be appreciated! Cheers!

try removing string declaration in your if (result.bSuccess) block.
In your case, the path and FN from the if block are local to the block and get undefined when you leave the block. On the other hand, the initial declarations never get any value.

So like this?

ofSoundPlayer sound;

string FN;
string path;
ofFileDialogResult result = ofSystemLoadDialog(“Load file”);
if (result.bSuccess) {
path = result.getPath();
FN = ofFilePath::getFileName(path);
}

sound.load(FN);
sound.play();
sound.setLoop(true);

Yes.
Beware that with this code you will also need to set FN to a default value when ofSystemLoadDialog fails.
If you’re not interested by FN elsewhere in your code then you could leave path and FN as local variable and put the sound commands in the if block. Something like :

ofSoundPlayer sound;

ofFileDialogResult result = ofSystemLoadDialog(“Load file”);
if (result.bSuccess) {
    string path = result.getPath();
    string FN = ofFilePath::getFileName(path);
    sound.load(FN);
    sound.play();
    sound.setLoop(true);
}

1 Like

Thank you very, very much!!!