QML多媒体播放文件夹中的所有mp3
QML Multimedia play all mp3 in a folder
我正在尝试在QML中构建一个简单的媒体播放器。我不能使用QFile
对话框,因为它是在EGLFS上运行的单个窗口应用程序。到目前为止,我设法为 QML 构建了一个简单的文件浏览器,并且可以从固定位置播放 mp3。但这就是我卡住的地方:
-
如何将树视图中的当前选定文件设置为音频源?
-
如何从所选文件夹中获取播放文件结尾为mp3的每个文件
Audio
?
感谢您的帮助
.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
主.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();
}
我的主目录 :
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
不是一个合适的选择(它是快速控制 1.0 元素在快速控制 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 ...等。
相关文章:
- DwmGetWindowAttribute 在全屏模式下无法获得媒体播放器的正确矩形大小
- 媒体基础 多个视频播放导致内存泄漏和崩溃
- 如何添加mp3文件作为资源,然后播放
- 如何在不显示媒体播放器的情况下在libVLC中拍摄快照
- QML多媒体播放文件夹中的所有mp3
- 使用 Struct 进行 mp3 播放器模拟C++(不需要播放 mp3 的唯一显示列表)
- 如何在c ++程序中播放或打开*.mp3或*.wav声音文件?
- 如何使用来自vlc媒体播放器的QNetworkAccessManager源代码
- 媒体基础全景(等距柱状投影)视频播放C++
- 在NACL中播放MP3文件
- 使用 C++ 在 Linux 中播放.mp3或其他音频文件
- QT/QML/C++媒体播放器
- 我正在尝试获取媒体播放器的元数据,但QT中不断出现C2228错误
- 暂停视频后C++如何访问构建器 6 媒体播放器对象中的当前帧
- 制作媒体播放器所需的输入
- 在 MFC 中嵌入窗口媒体播放器
- C++BASS库-简单的mp3示例播放
- 管理MP3的播放速度和位置
- 如何使用ffmpegc API对H.264视频进行编码,然后用媒体播放器打开输出
- 媒体播放列表的数据结构