当然,如果我在 qrc 文件中包含特定的媒体文件,那么下面的 qml 程序似乎可以检测到它。
我希望用户通过文件浏览器选择他想要的媒体文件,然后播放。
所以,目前,我以字符串的形式传递了文件路径,如下所示:
import QtQuick 2.0
import QtMultimedia 5.5
Rectangle
{
id: head
width: 500
height: 500
property int heightOfButtons: 50
property int widthOfButtons: 50
property string videoSource: "/home/*****/1.mp4"
Video
{
id: video
width : head.width
height : head.height
source: head.videoSource
MouseArea
{
anchors.fill: parent
onClicked: { video.play() }
}
onErrorStringChanged: console.log("errorstring: " + errorString)
focus: true
}
Grid
{
columns: 1; columnSpacing: 10
Rectangle
{
height: head.heightOfButtons; width: head.widthOfButtons; color: "green"
id: playStop
MouseArea {anchors.fill: parent; onClicked: video.playbackState == MediaPlayer.PlayingState ? video.pause() : video.play()}
Text {color: "white"; text: "Play/Pause"; anchors.right: parent.right; anchors.horizontalCenter: parent.horizontalCenter}
}
Rectangle
{
height: rightGrid.heightOfButtons; width: rightGrid.widthOfButtons; color: "red"
id: stop
MouseArea {anchors.fill: parent; onClicked: video.stop()}
Text {color: "white"; text: "Stop"; anchors.right: parent.right; anchors.horizontalCenter: parent.horizontalCenter;}
}
}
}
文件存在,但程序显示错误:
qml: errorstring: Attempting to play invalid Qt resource
如何播放用户动态选择的文件?