[英]QML Multimedia play all mp3 in a folder
我正在嘗試在QML中構建一個簡單的媒體播放器。 我不能使用QFile
Dialog作為在EGLFS上運行的單個窗口應用程序。 到目前為止,我設法為QML構建了一個簡單的文件瀏覽器,並且可以從固定位置播放mp3。 但是,這就是我遇到的問題:
如何將樹視圖中當前選擇的文件設置為音頻源?
如何從選定的文件夾中讓Audio
播放以mp3結尾的文件的每個文件?
謝謝你的幫助
.pro文件
QT += qml quick multimedia widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
DEFINES += QT_DEPRECATED_WARNING
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp中:
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml>
#include <QFileSystemModel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("Power-Tune");
app.setOrganizationDomain("power-tune.org");
app.setApplicationName("PowerTune");
QQmlApplicationEngine engine;
//
QFileSystemModel model;
model.setRootPath("/");
engine.rootContext()->setContextProperty("my_model", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
我的main.qml:
import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtMultimedia 5.8
ApplicationWindow {
visible: true
width: 800
height: 480
minimumWidth: 800
minimumHeight: 480
title: qsTr("PowerTune ")
// visibility: "FullScreen"
color: "black"
Rectangle {
width: parent.width
height: parent.height
property bool playing: false
Audio {
id: playMusic
//source: currently selected file in TreeView
}
Button {
id: previous
text: "previous"
width: 100
anchors.right: playpause.left
// onClicked: select previous item in current folder
}
Button {
id: playpause
text: "play/pause" //playing ? "Stop music" : "Start music"
width: 100
anchors.right: next.left
onClicked: {
if(playing == true) {
playMusic.stop()
playing = false
} else {
playMusic.play()
playing = true
}
}
}
Button {
id: next
text: "next"
width: 100
anchors.right: parent.right
}
TreeView {
id:mp3selector
width: parent.width/2
height: parent.height
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
model: my_model
}
}
}
幾個注意事項:
property bool playing: false
最好在層次結構的頂部定義屬性,以便在所有子級中都可以訪問它們,因此最好將其直接放在ApplicationWindow
而不放在Rectangle
元素中。
而且,我認為TreeView
不是一個合適的選擇(它的Quick Controls 1.0元素在Quick Controls 2.0中不可用,並且要混合使用,請查看此信息 );
您可以直接從QML制作模型,使用帶有FolderListModel
的ListView
,只需要使用鼠標即可突出顯示和選擇文件的其他裝飾。就是如此, TreeView
可以用下面的代碼替換,可以正常工作!
...
import QtQuick.Controls 2.2
import Qt.labs.folderlistmodel 2.1
...
ListView {
id: list
width: parent.width/2
height: parent.height
model: folderModel
onCurrentIndexChanged: {
// This will handle changing playlist with all possible selection methods
playMusic.source = folderModel.get(currentIndex, "fileURL")
}
FolderListModel {
id: folderModel
folder: "file:///MP3/"
showDirs: false
nameFilters: ["*.mp3"]
}
delegate: Component {
Item {
width: parent.width
height: 40
Column {
Text { text: fileName }
}
MouseArea {
anchors.fill: parent
onClicked: {
list.currentIndex = index
}
}
}
}
highlight: Rectangle {
color: 'grey'
}
focus: true
}
對於您的按鈕onClicked
只需處理ListView
currentIndex
,例如在下一個按鈕中添加:
onClicked: list.currentIndex += 1
您還需要添加一些代碼,以避免索引超出范圍或-1 ... etc。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.