You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QML多媒体播放器开发问题:设置音频源及播放文件夹内MP3

QML Multimedia Player Solutions for EGLFS Environment

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 currentIndex before 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:

  1. Detect when a folder is selected in the TreeView
  2. Fetch all MP3 files in that folder
  3. 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:

  • FolderListModel handles asynchronous directory scanning, so we use its onCountChanged signal to populate the playlist once all files are loaded.
  • You can adjust sorting (e.g., by date modified) by changing the sorting property of FolderListModel.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:09:41