I am trying to create a simple video player that just plays a specified video on loop. While the video plays as expected, it does not loop.
The following 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 call playVideo from C++. It starts playing as expected. However, once it completes, the frame freezes on the last one. I tried looping it by calling the loopVideo function in a QTimer. That does not work either.
What might I be doing wrong?
Your code is ok. (slight tip: you might want to use
MediaPlayer.Infiniteinstead100for looping)I believe that your situation is same like mine.
I have played a little with
MediaPlayercomponent and at my end I am unable to seek video becauseseekableis alwaysfalse. Andseekableisfalsebecause somehow QML uses my file as live stream, and that resultsdurationproperty to be0. Also note thatonPausedandonStoppedare never triggered andpositionis just increasing after end of video (live stream never ends).Now I think that this is related to looping, because basically looping seeks back to 0. Because there is no
duration(MediaPlayerthinks it is live stream) it cannot seek (and loop).One nasty workaround that I found is this (append to your code):
Where
VIDEO_LENGTHis length of your video file in milliseconds.Click here for MediaPlayer element documentation
UPDATE: It looks like that is bug in Qt for mingw version (closed bug report).
UPDATE 2: I have downloaded MSVC version of Qt and media player works as it is supposed.
So it is bug in Qt for mingw.
Either use this workaround (which I wouldn't recommend) or download MSVC version.
I have created new bug report here.