Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to use Qmediaplaylist to add all the audio files from a directory?
QtWS25 Last Chance

How to use Qmediaplaylist to add all the audio files from a directory?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 4 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronak5
    wrote on last edited by Ronak5
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      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()));
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      R 1 Reply Last reply
      2
      • VRoninV VRonin
        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()));
        }
        
        R Offline
        R Offline
        Ronak5
        wrote on last edited by Ronak5
        #3

        @VRonin thanks for quick response.
        bit this isn't working. What if I intend to provide an absolute path?

        jsulmJ 1 Reply Last reply
        0
        • R Ronak5

          @VRonin thanks for quick response.
          bit this isn't working. What if I intend to provide an absolute path?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          2
          • jsulmJ jsulm

            @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.

            R Offline
            R Offline
            Ronak5
            wrote on last edited by Ronak5
            #5

            @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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Could you replace playlist->addMedia(QUrl::fromLocalFile(it.filePath())); with

              if(!playlist->addMedia(QUrl::fromLocalFile(it.filePath())))
              qDebug() << it.filePath();
              

              And post the output?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              R 1 Reply Last reply
              0
              • VRoninV VRonin

                Could you replace playlist->addMedia(QUrl::fromLocalFile(it.filePath())); with

                if(!playlist->addMedia(QUrl::fromLocalFile(it.filePath())))
                qDebug() << it.filePath();
                

                And post the output?

                R Offline
                R Offline
                Ronak5
                wrote on last edited by
                #7

                @VRonin hey, while debug I figured it.hasNext() is being returned as false.
                and so no files have been added in playlist.

                jsulmJ 1 Reply Last reply
                0
                • R Ronak5

                  @VRonin hey, while debug I figured it.hasNext() is being returned as false.
                  and so no files have been added in playlist.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ronak5 Can you show the code you have now?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Ronak5 Can you show the code you have now?

                    R Offline
                    R Offline
                    Ronak5
                    wrote on last edited by Ronak5
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

                      Shouldn't that be:

                      QStringList() << "*.mp3" << "*.MP3"

                      ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      R 1 Reply Last reply
                      2
                      • SGaistS SGaist

                        Hi,

                        Shouldn't that be:

                        QStringList() << "*.mp3" << "*.MP3"

                        ?

                        R Offline
                        R Offline
                        Ronak5
                        wrote on last edited by
                        #11

                        @SGaist
                        hi, yes it is. it was an editor issue. A space helped it. Thanks for edit.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved