- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 QML 中构建一个简单的媒体播放器。我不能将 QFile
Dialog 用作在 EGLFS 上运行的单窗口应用程序。到目前为止,我设法为 QML 构建了一个简单的文件浏览器,并且可以从固定位置播放 mp3。但这就是我被困的地方:
如何将 TreeView 中的当前选定文件设置为我的音频源?
如何让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
主要.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 2.0 中不可用的 Quick Controls 1.0 元素,要混合请检查此 post );
您可以直接从 QML 创建模型,使用 ListView
和 FolderListModel
,您只需要额外的装饰来突出显示和使用鼠标选择文件..那是所有,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
,例如在 next 按钮中添加:
onClicked: list.currentIndex += 1
您还需要添加一些代码以避免索引超出范围或 -1 ...等。
关于c++ - QML多媒体播放文件夹中的所有mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49725623/
我修改了.pro: QT += core gui multimedia multimediawidgets 我包括 , 在 main.cpp 中 然后我尝试构建。它给了我以下错误: cannot f
我修改了.pro: QT += core gui multimedia multimediawidgets 我包括 , 在 main.cpp 中 然后我尝试构建。它给了我以下错误: cannot f
我也刚刚升级了 ubuntu 16.10,现在我在使用 QtMultimedia 5.7 时遇到了一些问题。 尝试从 qml 访问相机时出现以下错误。 (main:8542): GLib-GObjec
好的,这就是交易。我在 Ubuntu 14.04 LTS 上,安装了 Qt 5.6、qtmultimedia5-dev、gstreamer0.10(和 1.0)和 libqtgstreamer-dev
在 Firebug 控制台上出现此错误 => (TypeError: window.matchMedia(...) 为 null) 当我点击 paywithcard 按钮时,我会在 firebug 控
我在将 Millenial Media(http://mmedia.com/) 广告定位在屏幕底部时遇到问题。有时它有效,但有时广告无缘无故地跳到屏幕中央。其他时候,广告只是从那个位置开始。有时,广告
我正在尝试使用 QMediaPlayer 类来播放视频,但一直出现此错误 - `GLib-GObject-WARNING **: cannot register existing type 'GstO
我正在研究 fgpas 软核的自定义指令选择算法,我需要一些 C 代码来测试我的系统。 我的要求是: 面向大量数字运算:数字/多媒体/信号处理 用 C(首选)或 C++(或任何 llvm 可以编译的语
我是一名优秀的程序员,十分优秀!