Unable to loop video using QML MediaPlayer

4.7k Views Asked by At

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?

2

There are 2 best solutions below

0
On

Your code is ok. (slight tip: you might want to use MediaPlayer.Infinite instead 100 for looping)

I believe that your situation is same like mine.

I have played a little with MediaPlayer component and at my end I am unable to seek video because seekable is always false. And seekable is false because somehow QML uses my file as live stream, and that results duration property to be 0. Also note that onPaused and onStopped are never triggered and position is 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 (MediaPlayer thinks it is live stream) it cannot seek (and loop).

One nasty workaround that I found is this (append to your code):

Rectangle {
    id: root
    //...

    MediaPlayer {
        //...
        onPositionChanged: {
            if (position > VIDEO_LENGTH) {
                root.stopVideoPlayback()
                root.playVideo(root.currentVideo)
            }
        }
    }
}

Where VIDEO_LENGTH is 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.

1
On

Using stopped signal try this code :

 MediaPlayer {
     id: mediaplayer
     source: "groovy_video.mp4"
     onStopped: play()
}