Thanks for helping but what is shown in demo doesn't help me.
In the demo there are only 2 examples, one with Fonts and the other with Drag and Drop that demonstrates the codes of Listbox. But this is not my case because I have some mp3 files in a directory and I want to load them into the listbox but I'm not able to do so. I have searched all the functions of the classes and I didn't find a function that loads them. As I said I have no problem creating the ListBox, I just can't load the mp3 files.
I guess one way could be to iterate through a directory (or directories), testing if the file extension is mp3, and adding the file names to the list if they are. You could probably use a regex or File::getFileExtension() to test the extensions.
How do I load and display these mp3 songs to the listbox ?
Maybe you should specify more precisely, what you mean by loading and displaying...
Did you want to show length, artist info etc? You can read metadata from the AudioFormatReader class: http://www.juce.com/doc/classAudioFormatReader#a8dfe48ed0e171928185c716811f5ecdb
I din't use this class yet, so I don't know how good the results meet your expectation, I've seen that the OGG format implements id3 tag attributes e.g. http://www.juce.com/doc/classOggVorbisAudioFormat#a531f4523711824414005c291cb9c697b , you have to try if MP3 also reads usefull metadata...
Or do you want to show the waveform? Then you can use the AudioThumbnail class: http://www.juce.com/doc/classAudioThumbnail
And for loading and playback see the JuceDemo unter Source/Demos/AudioPlaybackExample.cpp: https://github.com/julianstorer/JUCE/blob/master/examples/Demo/Source/Demos/AudioPlaybackDemo.cpp
I hope that points to something useful, otherwise please rephrase your question... ;-)
class Track : public Component, public ListBoxModel
{
public:
Track()
: iterator(File("C:\\Users\\Gerald\\Music"), true, "*.mp3")
{
addAndMakeVisible(listBoxTra);
listBoxTra.setColour(ListBox::backgroundColourId, Colours::black.brighter(0.18f));
listBoxTra.setRowHeight(20);
listBoxTra.setModel(this);
listBoxTra.updateContent();
}
~Track()
{
}
void resized()
{
listBoxTra.setBounds(0, 0, getParentWidth(), getParentHeight());
}
int getNumRows()
{
File folder("C:\\Users\\Gerald\\Music");
return folder.getNumberOfChildFiles(File::findFiles, "*.mp3");
}
void paintListBoxItem(int rowNumber, Graphics& a,
int width, int height, bool rowIsSelected)
{
if (!rowIsSelected) {
a.setColour(Colour(0, 127, 255).brighter(0.3f));
}
else
if (rowIsSelected) {
a.fillAll(Colour(0, 127, 255).brighter(0.08f));
a.setColour(Colours::white);
}
a.setFont(height * 0.695f);
a.drawText(" The title of the song or mp3 file should be here but with the iterator class I can't get the name of the mp3 file. ",
5, 0, width, height,
Justification::centredLeft, true);
}
private:
ListBox listBoxTra;
DirectoryIterator iterator;
};
I can get the number of rows depending on the number of the mp3 files that I have in this folder
("C:\\Users\\Gerald\\Music") , but I don't know how to add them in the ListBox. In this " C:\\..." directory there are 14 mp3 files that I want to add into the listbox and I want to add them all and display their name.
When the user clicks on one of the songs in the list it should start playing.
I guess DirectoryIterator doesn't help me because it gets only the file that the iterator is currently pointing at.
Maybe there is something else that I'm missing... ?
I used DirectoryIterator Class but it didn't help me.
File::getFileExtension() only shows the extension of the file. What I want to do is that I need to show the names of all the mp3 files I have in that directory that I have set.
Use a File class object. Pass it the directory you want to search when you create it. Then call its findChildFiles()* method to populate a file array with the files from that directory. You can then use the names of these files to populate your list box.