Unable to preview video using QML MediaPlayer
I am trying to create a simple video player that simply plays the specified video in a loop. As long as the video plays as expected, it doesn't loop.
Below is the code I am using:
import QtQuick 2.0
import QtMultimedia 5.0
Rectangle
{
width : 320
height : 240
signal buttonPressed(string msg)
property string currentVideo
function playVideo(videoName)
{
currentVideo = videoName
videoPlayer.source = videoName
videoPlayer.seek(1)
videoPlayer.play()
}
function loopVideo()
{
if(videoPlayer.duration === 0)
{
playVideo(currentVideo)
}
}
function stopVideoPlayback()
{
videoPlayer.stop()
}
MediaPlayer {
id: videoPlayer
source: ""
autoPlay: false
autoLoad: false
loops: 100
}
VideoOutput {
id: videoOutput
source: videoPlayer
anchors.fill: parent
visible: true
}
}
I am calling playVideo
from C ++. He starts playing as expected. However, once it finishes, the frame freezes on the last one. I tried to loop it by calling the loopVideo function in QTimer. This doesn't work either.
What could I be doing wrong?
source to share
Your code is fine. (small tip: you can use MediaPlayer.Infinite
instead 100
for for loop)
I believe your situation is the same as mine.
I played around with the component a bit MediaPlayer
and in the end I can't search for videos because seekable
always false
. And seekable
it matters false
because somehow QML is using my file as a live stream and this causes the property to duration
be 0
. Also note that onPaused
and onStopped
never start, but position
just increases after the video ends (live stream never ends).
Now I think it has to do with the loop, because basically the loop goes back to 0. Since it is not duration
( MediaPlayer
thinks it is a direct stream), it cannot search (and the loop).
One nasty workaround I found is this (add to your code):
Rectangle {
id: root
//...
MediaPlayer {
//...
onPositionChanged: {
if (position > VIDEO_LENGTH) {
root.stopVideoPlayback()
root.playVideo(root.currentVideo)
}
}
}
}
Where VIDEO_LENGTH
is the length of your video file in milliseconds.
Click here for documentation of MediaPlayer controls
UPDATE: Looks like this is a bug in Qt for mingw version (closed bug report).
UPDATE 2: I downloaded the MSVC version of Qt and the media player works as expected.
So this is a bug in Qt for mingw .
Use this workaround (which I would not recommend) or download the MSVC version .
source to share