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?
Qt 6.11 is out! See what's new in the release blog

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 4.1k 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.
  • 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