How Can i get all audio files in any folder from root folder?
-
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?
-
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?
@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
-
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 -
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. -
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.@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
-
@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
@eyllanesc Thank You So much :)
-
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.@EmadDeve20 PyQt5 or PySide2?
-
@EmadDeve20 PyQt5 or PySide2?
@eyllanesc PyQt5
-
@eyllanesc PyQt5
@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() }
-
@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() }
@eyllanesc how can I say thank you?
i go to testing this :D -
@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() }
-
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!
-
I think I am a super noob!
I know no what's happening!
I know no why this does not work! -
I think I am a super noob!
I know no what's happening!
I know no why this does not work!@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.
-
@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() }
@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. -
@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.
@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.
-
@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.
@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.
-
@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.@eyllanesc You'r right.
But I just censored my username!
In real code, I use my own username. -
@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.
@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