How to use Qmediaplaylist to add all the audio files from a directory?
-
Hello,
instead of adding individual audio files like playlist->addMedia(QUrl("http://example.com/movie1.mp4"));How can I add complete audio files from a directory in QmediaPlaylist?
-
QDirIterator it(QStandardPaths::standardLocations(QStandardPaths::MusicLocation).first(),{QStringLiteral(".mp4"),QStringLiteral(".mp3"),QStringLiteral(".wav"),QStringLiteral(".flac")},QDir::Files|QDir::Readable,QDirIterator::NoIteratorFlags); while (it.hasNext()) { it.next(); playlist->addMedia(QUrl::fromLocalFile(it.filePath())); }
-
@Ronak5 said in How to use Qmediaplaylist to add all the audio files from a directory?:
What if I intend to provide an absolute path?
Well, then use the absolute path:
QDirIterator it("YOUR_PATH_HERE",{QStringLiteral(".mp4"),QStringLiteral(".mp3"),QStringLiteral(".wav"),QStringLiteral(".flac")},QDir::Files|QDir::Readable,QDirIterator::NoIteratorFlags); while (it.hasNext()) { it.next(); playlist->addMedia(QUrl::fromLocalFile(it.filePath())); }
If it is not working then please say what exactly is not working.
-
@jsulm hey thanks, I tried both now.
But I'm getting an error after that..I added few files using:
QDir directory("/home/guest/Music"); QStringList musicFiles = directory.entryList(QStringList() << "*.mp3" << "*.MP3",QDir::Files|QDir::Readable); foreach(QString filename, musicFiles){ playlist->addMedia(QMediaContent(QUrl::fromLocalFile(filename)));
GStreamer; Unable to pause - "file:bensound-anewbeginning.mp3"
Error: "Could not open resource for reading."What am I missing here? Is there any permission i need here.
-
@jsulm
So now I'm working with this. I had added absolute path in this line to add files (QUrl::fromLocalFile("/home/guest/Music/"+filename)));QDir directory("/home/guest/Music");
QStringList musicFiles = directory.entryList(QStringList() << " *.mp3" << " *.MP3",QDir::Files|QDir::Readable);foreach(QString filename, musicFiles){ playlist->addMedia(QMediaContent(QUrl::fromLocalFile("/home/guest/Music/"+filename))); }
With this I'm able to add files now and perform operations on it.
-
Hi,
Shouldn't that be:
QStringList() << "*.mp3" << "*.MP3"
?