Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How Can i get all audio files in any folder from root folder?
Forum Updated to NodeBB v4.3 + New Features

How Can i get all audio files in any folder from root folder?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
19 Posts 3 Posters 2.3k 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.
  • E Offline
    E Offline
    EmadDeve20
    wrote on last edited by EmadDeve20
    #1

    hello everybody!
    I am trying to create a music player for a car with qml on orangepi.
    I need to get any audio files from a flash drive. the music files can be inside any folder, but I need to get these.

    this part of my code:

    MediaPlayer {
               id: player
               autoPlay: true
             
           }
    
           Item {
               id: playLogic
    
               property int index: -1
               property MediaPlayer mediaPlayer: player
               property FolderListModel items: FolderListModel {
                   //folder: "./music"
                   folder: "/media/usb/"
                   nameFilters: ["*.mp3", "*.ogg", "*.wav"]
                   showDirs: false
                   showFiles: true
               }
    
               function init(){
                   if(mediaPlayer.playbackState===1){
                       mediaPlayer.pause();
                   }else if(mediaPlayer.playbackState===2){
                       mediaPlayer.play();
                   }else{
                       setIndex(0);
                   }
               }
    
               function setIndex(i)
               {
                   index = i;
    
                   if (index < 0 || index >= items.count)
                   {
                       index = -1;
                       mediaPlayer.source = "";
                   }
                   else{
                       mediaPlayer.source = items.get(index,"filePath");
                       mediaPlayer.play();
                   }
               }
    
               function next(){
                   setIndex(index + 1);
                   player.play()
               }
    
               function previous(){
                   setIndex(index - 1);
                   player.play()
               }
    
               Connections {
                   target: playLogic.mediaPlayer
    
                   onPaused: {
                       playpuse_img.source = puse;
                   }
    
                   onPlaying: {
                        playpuse_img.source = play;
                   }
    
                   onStopped: {
                       playpuse_img.source = play;
                       if (playLogic.mediaPlayer.status == MediaPlayer.EndOfMedia)
                           playLogic.next();
                   }
    
                   onError: {
                       console.log(error+" error string is "+errorString);
                   }
    
                   onMediaObjectChanged: {
                       if (playLogic.mediaPlayer.mediaObject)
                           playLogic.mediaPlayer.mediaObject.notifyInterval = 50;
                   }
               }
    
           }
    
    

    how can I do that?

    eyllanescE 1 Reply Last reply
    0
    • E EmadDeve20

      hello everybody!
      I am trying to create a music player for a car with qml on orangepi.
      I need to get any audio files from a flash drive. the music files can be inside any folder, but I need to get these.

      this part of my code:

      MediaPlayer {
                 id: player
                 autoPlay: true
               
             }
      
             Item {
                 id: playLogic
      
                 property int index: -1
                 property MediaPlayer mediaPlayer: player
                 property FolderListModel items: FolderListModel {
                     //folder: "./music"
                     folder: "/media/usb/"
                     nameFilters: ["*.mp3", "*.ogg", "*.wav"]
                     showDirs: false
                     showFiles: true
                 }
      
                 function init(){
                     if(mediaPlayer.playbackState===1){
                         mediaPlayer.pause();
                     }else if(mediaPlayer.playbackState===2){
                         mediaPlayer.play();
                     }else{
                         setIndex(0);
                     }
                 }
      
                 function setIndex(i)
                 {
                     index = i;
      
                     if (index < 0 || index >= items.count)
                     {
                         index = -1;
                         mediaPlayer.source = "";
                     }
                     else{
                         mediaPlayer.source = items.get(index,"filePath");
                         mediaPlayer.play();
                     }
                 }
      
                 function next(){
                     setIndex(index + 1);
                     player.play()
                 }
      
                 function previous(){
                     setIndex(index - 1);
                     player.play()
                 }
      
                 Connections {
                     target: playLogic.mediaPlayer
      
                     onPaused: {
                         playpuse_img.source = puse;
                     }
      
                     onPlaying: {
                          playpuse_img.source = play;
                     }
      
                     onStopped: {
                         playpuse_img.source = play;
                         if (playLogic.mediaPlayer.status == MediaPlayer.EndOfMedia)
                             playLogic.next();
                     }
      
                     onError: {
                         console.log(error+" error string is "+errorString);
                     }
      
                     onMediaObjectChanged: {
                         if (playLogic.mediaPlayer.mediaObject)
                             playLogic.mediaPlayer.mediaObject.notifyInterval = 50;
                     }
                 }
      
             }
      
      

      how can I do that?

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @EmadDeve20 FolderListModel will not help as it only handles one folder, and not the subfolders. You have to implement the logic with C++, a similar example can be found in the following post: https://stackoverflow.com/questions/56406220/how-do-i-populate-a-playlist-qml-type-using-a-c-model/56411623#56411623

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      2
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        I'm sure there is a way to do it in qml/js but I don't know how.

        The Qt way to do it would be with QDir - with root set as start path - and QDirIterator.

        here's an c++/Qwidget example that could get you startet:
        https://doc.qt.io/qt-5/qtwidgets-dialogs-findfiles-example.html


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • E Offline
          E Offline
          EmadDeve20
          wrote on last edited by
          #4

          Thank you @eyllanesc and @J-Hilk .
          Now I need to try for this. because I using python backend and cpp is hard for me.
          any way Thanks.

          eyllanescE 2 Replies Last reply
          1
          • E EmadDeve20

            Thank you @eyllanesc and @J-Hilk .
            Now I need to try for this. because I using python backend and cpp is hard for me.
            any way Thanks.

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by
            #5

            @EmadDeve20 If you are using python then you should point it out, but the logic is the same. I am going to translate one of the several solutions that I post

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            E 1 Reply Last reply
            1
            • eyllanescE eyllanesc

              @EmadDeve20 If you are using python then you should point it out, but the logic is the same. I am going to translate one of the several solutions that I post

              E Offline
              E Offline
              EmadDeve20
              wrote on last edited by
              #6

              @eyllanesc Thank You So much :)

              1 Reply Last reply
              0
              • E EmadDeve20

                Thank you @eyllanesc and @J-Hilk .
                Now I need to try for this. because I using python backend and cpp is hard for me.
                any way Thanks.

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by
                #7

                @EmadDeve20 PyQt5 or PySide2?

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                E 1 Reply Last reply
                1
                • eyllanescE eyllanesc

                  @EmadDeve20 PyQt5 or PySide2?

                  E Offline
                  E Offline
                  EmadDeve20
                  wrote on last edited by
                  #8

                  @eyllanesc PyQt5

                  eyllanescE 1 Reply Last reply
                  0
                  • E EmadDeve20

                    @eyllanesc PyQt5

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on last edited by eyllanesc
                    #9

                    @EmadDeve20 Demo:

                    import os
                    import sys
                    from pathlib import Path
                    import threading
                    
                    from PyQt5.QtCore import (
                        QCoreApplication,
                        QDir,
                        QDirIterator,
                        QObject,
                        Qt,
                        QUrl,
                        pyqtSignal,
                        pyqtSlot,
                    )
                    from PyQt5.QtGui import QGuiApplication
                    from PyQt5.QtQml import QQmlApplicationEngine
                    
                    CURRENT_DIRECTORY = Path(__file__).resolve().parent
                    
                    
                    class PlayerHelper(QObject):
                        loaded = pyqtSignal(list)
                    
                        @pyqtSlot(str, list)
                        def load(self, directory, filters):
                            threading.Thread(
                                target=self._load, args=(directory, filters), daemon=True
                            ).start()
                    
                        def _load(self, directory, filters):
                            sources = []
                            it = QDirIterator(directory, filters, QDir.Files, QDirIterator.Subdirectories)
                            while it.hasNext():
                                sources.append(QUrl.fromLocalFile(it.next()))
                            self.loaded.emit(sources)
                    
                    
                    def main():
                        app = QGuiApplication(sys.argv)
                        engine = QQmlApplicationEngine()
                    
                        player_helper = PlayerHelper()
                    
                        engine.rootContext().setContextProperty("playerHelper", player_helper)
                        filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
                        url = QUrl.fromLocalFile(filename)
                    
                        def handle_object_created(obj, obj_url):
                            if obj is None and url == obj_url:
                                QCoreApplication.exit(-1)
                    
                        engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
                        engine.load(url)
                    
                        ret = app.exec_()
                    
                        sys.exit(ret)
                    
                    
                    if __name__ == "__main__":
                        main()
                    
                    import QtQuick 2.15
                    import QtQuick.Controls 2.15
                    import QtMultimedia 5.15
                    
                    ApplicationWindow {
                        id: root
                    
                        width: 640
                        height: 480
                        visible: true
                    
                        ListView {
                            model: playlist
                            anchors.fill: parent
                    
                            delegate: Text {
                                font.pixelSize: 16
                                text: source
                            }
                    
                        }
                    
                        QtObject {
                            id: internals
                    
                            function load() {
                                playlist.clear();
                                playerHelper.load("/media/usb/", ["*.mp3", "*.ogg", "*.wav"]);
                            }
                    
                        }
                    
                        MediaPlayer {
                            id: player
                    
                            playlist: Playlist {
                                id: playlist
                            }
                    
                        }
                    
                        Connections {
                            function onLoaded(sources) {
                                playlist.addItems(sources);
                                player.play();
                            }
                    
                            target: playerHelper
                        }
                    
                        Component.onCompleted: internals.load()
                    
                    }
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    E 1 Reply Last reply
                    1
                    • eyllanescE eyllanesc

                      @EmadDeve20 Demo:

                      import os
                      import sys
                      from pathlib import Path
                      import threading
                      
                      from PyQt5.QtCore import (
                          QCoreApplication,
                          QDir,
                          QDirIterator,
                          QObject,
                          Qt,
                          QUrl,
                          pyqtSignal,
                          pyqtSlot,
                      )
                      from PyQt5.QtGui import QGuiApplication
                      from PyQt5.QtQml import QQmlApplicationEngine
                      
                      CURRENT_DIRECTORY = Path(__file__).resolve().parent
                      
                      
                      class PlayerHelper(QObject):
                          loaded = pyqtSignal(list)
                      
                          @pyqtSlot(str, list)
                          def load(self, directory, filters):
                              threading.Thread(
                                  target=self._load, args=(directory, filters), daemon=True
                              ).start()
                      
                          def _load(self, directory, filters):
                              sources = []
                              it = QDirIterator(directory, filters, QDir.Files, QDirIterator.Subdirectories)
                              while it.hasNext():
                                  sources.append(QUrl.fromLocalFile(it.next()))
                              self.loaded.emit(sources)
                      
                      
                      def main():
                          app = QGuiApplication(sys.argv)
                          engine = QQmlApplicationEngine()
                      
                          player_helper = PlayerHelper()
                      
                          engine.rootContext().setContextProperty("playerHelper", player_helper)
                          filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
                          url = QUrl.fromLocalFile(filename)
                      
                          def handle_object_created(obj, obj_url):
                              if obj is None and url == obj_url:
                                  QCoreApplication.exit(-1)
                      
                          engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
                          engine.load(url)
                      
                          ret = app.exec_()
                      
                          sys.exit(ret)
                      
                      
                      if __name__ == "__main__":
                          main()
                      
                      import QtQuick 2.15
                      import QtQuick.Controls 2.15
                      import QtMultimedia 5.15
                      
                      ApplicationWindow {
                          id: root
                      
                          width: 640
                          height: 480
                          visible: true
                      
                          ListView {
                              model: playlist
                              anchors.fill: parent
                      
                              delegate: Text {
                                  font.pixelSize: 16
                                  text: source
                              }
                      
                          }
                      
                          QtObject {
                              id: internals
                      
                              function load() {
                                  playlist.clear();
                                  playerHelper.load("/media/usb/", ["*.mp3", "*.ogg", "*.wav"]);
                              }
                      
                          }
                      
                          MediaPlayer {
                              id: player
                      
                              playlist: Playlist {
                                  id: playlist
                              }
                      
                          }
                      
                          Connections {
                              function onLoaded(sources) {
                                  playlist.addItems(sources);
                                  player.play();
                              }
                      
                              target: playerHelper
                          }
                      
                          Component.onCompleted: internals.load()
                      
                      }
                      
                      E Offline
                      E Offline
                      EmadDeve20
                      wrote on last edited by
                      #10

                      @eyllanesc how can I say thank you?
                      i go to testing this :D

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        EmadDeve20
                        wrote on last edited by EmadDeve20
                        #11

                        @eyllanesc sorry, can I ask my code is true?
                        why this is not working?

                        import os
                        import sys
                        from pathlib import Path
                        import threading
                        
                        from PyQt5.QtCore import (
                            QCoreApplication,
                            QDir,
                            QDirIterator,
                            QObject,
                            Qt,
                            QUrl,
                            pyqtSignal,
                            pyqtSlot,
                        )
                        from PyQt5.QtGui import QGuiApplication
                        from PyQt5.QtQml import QQmlApplicationEngine
                        
                        CURRENT_DIRECTORY = Path(__file__).resolve().parent
                        
                        
                        class PlayerHelper(QObject):
                            loaded = pyqtSignal(list)
                        
                            @pyqtSlot(str, list)
                            def load(self, directory, filters):
                                threading.Thread(
                                    target=self._load, args=("/home/$USER/test_music/", ["*.mp3", "*.ogg", "*.wav"]), daemon=True
                                ).start()
                        
                            def _load(self, directory, filters):
                                sources = []
                                it = QDirIterator(directory, filters, QDir.Files, QDirIterator.Subdirectories)
                                while it.hasNext():
                                    sources.append(QUrl.fromLocalFile(it.next()))
                                self.loaded.emit(sources)
                        
                        
                        def main():
                            app = QGuiApplication(sys.argv)
                            engine = QQmlApplicationEngine()
                        
                            player_helper = PlayerHelper()
                        
                            engine.rootContext().setContextProperty("playerHelper", player_helper)
                            filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
                            url = QUrl.fromLocalFile(filename)
                        
                            def handle_object_created(obj, obj_url):
                                if obj is None and url == obj_url:
                                    QCoreApplication.exit(-1)
                        
                            engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
                            engine.load(url)
                        
                            ret = app.exec_()
                        
                            sys.exit(ret)
                        
                        
                        if __name__ == "__main__":
                            main()
                        
                        
                        import QtQuick 2.12
                        import QtQuick.Controls 2.12
                        import QtMultimedia 5.12
                        
                        ApplicationWindow {
                            id: root
                        
                            width: 640
                            height: 480
                            visible: true
                        
                            ListView {
                                model: playlist
                                anchors.fill: parent
                        
                                delegate: Text {
                                    font.pixelSize: 16
                                    text: source
                                }
                        
                            }
                        
                            QtObject {
                                id: internals
                        
                                function load() {
                                    playlist.clear();
                                    playerHelper.load("/media/usb/", ["*.mp3", "*.ogg", "*.wav"]);
                                }
                        
                            }
                        
                            MediaPlayer {
                                id: player
                        
                                playlist: Playlist {
                                    id: playlist
                                }
                        
                            }
                        
                            Connections {
                                function onLoaded(sources) {
                                    playlist.addItems(sources);
                                    player.play();
                                }
                        
                                target: playerHelper
                            }
                        
                            Component.onCompleted: internals.load()
                        
                        }
                        
                        
                        eyllanescE 1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          EmadDeve20
                          wrote on last edited by
                          #12

                          and this is

                          @pyqtSlot(str, list)
                              def load(self, directory="/home/$USER/test_music/", filters=["*.mp3", "*.ogg", "*.wav"]):
                                  threading.Thread(
                                      target=self._load, args=(directory, filters), daemon=True
                                  ).start()
                          

                          not working!

                          1 Reply Last reply
                          0
                          • E Offline
                            E Offline
                            EmadDeve20
                            wrote on last edited by
                            #13

                            I think I am a super noob!
                            I know no what's happening!
                            I know no why this does not work!

                            eyllanescE 1 Reply Last reply
                            0
                            • E EmadDeve20

                              I think I am a super noob!
                              I know no what's happening!
                              I know no why this does not work!

                              eyllanescE Offline
                              eyllanescE Offline
                              eyllanesc
                              wrote on last edited by eyllanesc
                              #14

                              @EmadDeve20 Please avoid placing I know repeatedly as it is annoying and I do not understand what you intend to achieve with that action. With those kinds of immature actions you only discourage me from helping you.

                              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                              E 1 Reply Last reply
                              0
                              • E EmadDeve20

                                @eyllanesc sorry, can I ask my code is true?
                                why this is not working?

                                import os
                                import sys
                                from pathlib import Path
                                import threading
                                
                                from PyQt5.QtCore import (
                                    QCoreApplication,
                                    QDir,
                                    QDirIterator,
                                    QObject,
                                    Qt,
                                    QUrl,
                                    pyqtSignal,
                                    pyqtSlot,
                                )
                                from PyQt5.QtGui import QGuiApplication
                                from PyQt5.QtQml import QQmlApplicationEngine
                                
                                CURRENT_DIRECTORY = Path(__file__).resolve().parent
                                
                                
                                class PlayerHelper(QObject):
                                    loaded = pyqtSignal(list)
                                
                                    @pyqtSlot(str, list)
                                    def load(self, directory, filters):
                                        threading.Thread(
                                            target=self._load, args=("/home/$USER/test_music/", ["*.mp3", "*.ogg", "*.wav"]), daemon=True
                                        ).start()
                                
                                    def _load(self, directory, filters):
                                        sources = []
                                        it = QDirIterator(directory, filters, QDir.Files, QDirIterator.Subdirectories)
                                        while it.hasNext():
                                            sources.append(QUrl.fromLocalFile(it.next()))
                                        self.loaded.emit(sources)
                                
                                
                                def main():
                                    app = QGuiApplication(sys.argv)
                                    engine = QQmlApplicationEngine()
                                
                                    player_helper = PlayerHelper()
                                
                                    engine.rootContext().setContextProperty("playerHelper", player_helper)
                                    filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
                                    url = QUrl.fromLocalFile(filename)
                                
                                    def handle_object_created(obj, obj_url):
                                        if obj is None and url == obj_url:
                                            QCoreApplication.exit(-1)
                                
                                    engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
                                    engine.load(url)
                                
                                    ret = app.exec_()
                                
                                    sys.exit(ret)
                                
                                
                                if __name__ == "__main__":
                                    main()
                                
                                
                                import QtQuick 2.12
                                import QtQuick.Controls 2.12
                                import QtMultimedia 5.12
                                
                                ApplicationWindow {
                                    id: root
                                
                                    width: 640
                                    height: 480
                                    visible: true
                                
                                    ListView {
                                        model: playlist
                                        anchors.fill: parent
                                
                                        delegate: Text {
                                            font.pixelSize: 16
                                            text: source
                                        }
                                
                                    }
                                
                                    QtObject {
                                        id: internals
                                
                                        function load() {
                                            playlist.clear();
                                            playerHelper.load("/media/usb/", ["*.mp3", "*.ogg", "*.wav"]);
                                        }
                                
                                    }
                                
                                    MediaPlayer {
                                        id: player
                                
                                        playlist: Playlist {
                                            id: playlist
                                        }
                                
                                    }
                                
                                    Connections {
                                        function onLoaded(sources) {
                                            playlist.addItems(sources);
                                            player.play();
                                        }
                                
                                        target: playerHelper
                                    }
                                
                                    Component.onCompleted: internals.load()
                                
                                }
                                
                                
                                eyllanescE Offline
                                eyllanescE Offline
                                eyllanesc
                                wrote on last edited by eyllanesc
                                #15

                                @EmadDeve20 If you want to change the directory then do it in QML : playerHelper.load("/home/qt_user/test_music/", ["*.mp3", "*.ogg", "*.wav"]);, Do not use $USER since this type of variable is not recognized but you must use the user's name.

                                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                                E 1 Reply Last reply
                                0
                                • eyllanescE eyllanesc

                                  @EmadDeve20 Please avoid placing I know repeatedly as it is annoying and I do not understand what you intend to achieve with that action. With those kinds of immature actions you only discourage me from helping you.

                                  E Offline
                                  E Offline
                                  EmadDeve20
                                  wrote on last edited by
                                  #16

                                  @eyllanesc I'm a little tired, I do not know what to do and I still have my work left. I apologize if I upset you. Not a good day for me.

                                  eyllanescE 1 Reply Last reply
                                  0
                                  • E EmadDeve20

                                    @eyllanesc I'm a little tired, I do not know what to do and I still have my work left. I apologize if I upset you. Not a good day for me.

                                    eyllanescE Offline
                                    eyllanescE Offline
                                    eyllanesc
                                    wrote on last edited by
                                    #17

                                    @EmadDeve20 If you are tired and apathetic then go to rest since with those actions you annoy us and you infect us with your apathy. Bye Bye.

                                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                                    E 1 Reply Last reply
                                    0
                                    • eyllanescE eyllanesc

                                      @EmadDeve20 If you want to change the directory then do it in QML : playerHelper.load("/home/qt_user/test_music/", ["*.mp3", "*.ogg", "*.wav"]);, Do not use $USER since this type of variable is not recognized but you must use the user's name.

                                      E Offline
                                      E Offline
                                      EmadDeve20
                                      wrote on last edited by
                                      #18

                                      @eyllanesc You'r right.
                                      But I just censored my username!
                                      In real code, I use my own username.

                                      1 Reply Last reply
                                      0
                                      • eyllanescE eyllanesc

                                        @EmadDeve20 If you are tired and apathetic then go to rest since with those actions you annoy us and you infect us with your apathy. Bye Bye.

                                        E Offline
                                        E Offline
                                        EmadDeve20
                                        wrote on last edited by
                                        #19

                                        @eyllanesc said in How Can i get all audio files in any folder from root folder?:

                                        If you are tired and apathetic then go to rest since with those actions you annoy us and you infect us with your apathy. Bye Bye.

                                        you right. i thank you so much :D

                                        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