QML多媒体播放器开发问题:设置音频源及播放文件夹内MP3
Hey there! I get that working in an EGLFS environment without QFileDialog adds some extra hoops, but let's work through your two key issues one by one.
1. Set TreeView Selected File as Audio Source
First, assuming you're using a FileSystemModel (or similar) to power your TreeView, you can directly bind the selected file's path to your Audio component's source property. Here's a concrete example:
// Define your file system model (filter MP3s upfront to simplify selection) FileSystemModel { id: fileSystemModel rootPath: "/your/fixed/mp3/path" nameFilters: ["*.mp3"] } TreeView { id: fileTree model: fileSystemModel selectionMode: SelectionMode.SingleSelection // Basic column for file names TableViewColumn { title: "MP3 Files" role: "fileName" width: 300 } // Update Audio source when selection changes onCurrentIndexChanged: { if (currentIndex.isValid) { const selectedFilePath = fileSystemModel.filePath(currentIndex); // Don't forget the "file://" prefix for local file paths in QML audioPlayer.source = `file://${selectedFilePath}`; // Optional: Auto-play when a file is selected // audioPlayer.play(); } } } Audio { id: audioPlayer autoPlay: false // Adjust based on your needs }
Key Notes:
- Always validate
currentIndexbefore accessing the file path to avoid errors if nothing is selected. - The
file://prefix is critical for QML to recognize local file system paths correctly.
2. Play All MP3s in a Selected Folder
To play all MP3s in a chosen folder, you'll need to:
- Detect when a folder is selected in the TreeView
- Fetch all MP3 files in that folder
- Maintain a playlist and auto-advance to the next track when one finishes
Here's how to implement this with pure QML (using FolderListModel to scan the directory):
// Track playlist state property var mp3Playlist: [] property int currentTrackIndex: 0 TreeView { id: fileTree model: fileSystemModel // ... (previous TreeView setup) onCurrentIndexChanged: { if (currentIndex.isValid) { const selectedPath = fileSystemModel.filePath(currentIndex); const selectedFileInfo = fileSystemModel.fileInfo(currentIndex); if (selectedFileInfo.isDir) { // Load all MP3s in the selected folder mp3Playlist = []; const folderScanner = Qt.createQmlObject(` import QtQuick 2.15; FolderListModel { folder: "file://${selectedPath}" nameFilters: ["*.mp3"] sorting: FolderListModel.Name } `, audioPlayer); // Wait for the scanner to finish loading files folderScanner.onCountChanged: { for (let i = 0; i < folderScanner.count; i++) { mp3Playlist.push(folderScanner.get(i).filePath); } // Start playing the first track if playlist isn't empty if (mp3Playlist.length > 0) { currentTrackIndex = 0; audioPlayer.source = `file://${mp3Playlist[currentTrackIndex]}`; audioPlayer.play(); } } } else if (selectedFileInfo.suffix.toLowerCase() === "mp3") { // Handle single file selection (reset playlist to just this file) mp3Playlist = [selectedPath]; currentTrackIndex = 0; audioPlayer.source = `file://${selectedPath}`; } } } } Audio { id: audioPlayer // Auto-play next track when current one ends onStopped: { if (mp3Playlist.length > currentTrackIndex + 1) { currentTrackIndex++; source = `file://${mp3Playlist[currentTrackIndex]}`; play(); } } }
Key Notes:
FolderListModelhandles asynchronous directory scanning, so we use itsonCountChangedsignal to populate the playlist once all files are loaded.- You can adjust sorting (e.g., by date modified) by changing the
sortingproperty ofFolderListModel.
Quick Check on .pro Configuration
Your existing .pro setup with QT += qml quick multimedia widgets is perfect for this use case—multimedia is required for the Audio component, and qml quick covers the QML UI elements.
内容的提问来源于stack exchange,提问作者Markus

