How to pause video on last frame in QML?

qml, qt, qt5, video

Solution

I'm still looking for a better solution to this. What I've come up with is to pause the video one second before it's over:

MediaPlayer {
    autoLoad: true
    id: video

    onPositionChanged: {
        if (video.position > 1000 && video.duration - video.position < 1000) {
            video.pause();
        }
    }
}

Why one second? On my machine if you try to pause it about 500ms before the end, the video manages to run to completion and disappear from view without even registering the `pause()` call. Thus 1 second is sort of a good safe value for me.

Frankly, I'd prefer if there was a more explicit way to tell the MediaPlayer what to do at the end of the video. I know for a fact that GStreamer, which is what Qt uses on Linux and Mac, notifies you when the video is almost over so that you can decide what to do next - e.g. pause the video or loop it seamlessly.

Problem

I'm trying to stop a QML video and show its last frame when playback has finished. Does anybody know how to do this? (Sorry, this seems to be not as trivial as it sounds...) At the moment, my problem is that the Video element simply becomes invisible/hidden after playback is done. (`onVisibleChanged` is never called.) When I use the hack in `onStatusChanged` in my code, the video disappears for a moment after the end and then shows the end of the video. What I'm doing is simply: ``` Video { anchors.fill: parent fillMode: VideoOutput.PreserveAspectFit; source: "path/to/file" autoPlay: true onStatusChanged: { console.warn("StatusChanged:"+status+"|"+MediaPlayer.Loaded) if (status == MediaPlayer.EndOfMedia) { // seek a bit before the end of the video since the last frames // are the same here, anyway seek(metaData.duration-200) play() pause() } } onVisibleChanged: { console.log(visible) } } ``` It's possible that I'm missing something, but I could not find anything on this topic in the docs. Also, using separate `MediaPlayer` and `VideoOutput` does not change the behavior. For the record, I'm using the latest Qt 5.2 on Windows (msvc2010+OpenGL-build).

Original source