[Windows, Qt5, QMediaPlayer, QMediaPlaylist]: Tiny

2019-09-12 18:09发布

问题:

I'm writing a Video Player with Qt5::QMediaPlayer to play randomly some videos for a randomly duration as this:

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    QMediaPlaylist* playlist = new QMediaPlaylist(&a);
    playlist->addMedia(QUrl::fromLocalFile("./Resources/fractal-files/A-060405V4651.WMV"));
    playlist->addMedia(QUrl::fromLocalFile("./Resources/fractal-files/E-102604.WMV"));
    playlist->addMedia(QUrl::fromLocalFile("./Resources/fractal-files/C-102304.WMV"));

    QMediaPlayer* player = new QMediaPlayer(&a);
    player->setPlaylist(playlist);

    QVideoWidget* videoWidget = new QVideoWidget;
    player->setVideoOutput(videoWidget);

    player->play();
    videoWidget->show();

    QTimer* t = new QTimer;
    QObject::connect(t, &QTimer::timeout, [&](){
        playlist->setCurrentIndex(playlist->nextIndex());
        player->play();
        videoWidget->setWindowTitle(playlist->currentMedia().canonicalUrl().fileName());
        t->start((qrand()%5 + 5)*1000);
    });
    t->start((qrand()%5 + 5)*1000);

    QTimer* t2 = new QTimer;
    QObject::connect(t2, &QTimer::timeout, [&](){
        player->setPosition(qrand() % player->duration());
        videoWidget->setWindowTitle(playlist->currentMedia().canonicalUrl().fileName());
        t2->start((qrand()%2 + 2)*1000);
    });
    t2->start((qrand()%2 + 2)*1000);

    return a.exec();
}

There are two issues:
1. When change the position

player->setPosition(qrand() % player->duration());

there are a bit delay (my client can accept this, but smoothly is better)
2. When the video source changed with:

playlist->setCurrentIndex(playlist->nextIndex());
player->play();

there are a small duration black screen that the client doesn't want. He want the effect at least same as when changing the postion:

player->setPosition(qrand() % player->duration());

Can we remove this black screen when changing the video source with Qt5 on Windows? Or we can do this with other libraries/frameworks (play a list of videos without black screen gap when change the video source)? (On MacOs, the switching is smooth)
Thank you very much!

回答1:

This is a reported bug in QtMediaPlayer and it only occurs on Windows. In current API, when a media reach the end, player clears the video area to play next media. Transition is not seamless and cause invalid frames. As a workaround in a looping video one can change the position to desired frame in same media.

If you need to change the media you can try using two mediaplayer object one for current media and one for next media. When it is time to play the next media change that player position to 0 and show it on widget. That one is stated in link of bug report. It is messy and still not a complete solution.

So, What to do?

  1. You can wait for the Qt fix. Bug seems reported 2-3 months ago.

  2. Change Media Player ( Gstreamer has gapless/seamless video support ) You can surely embed a media player in your UI developped with Qt. There are examples.

  3. Move t another UI library that offers better media playing features.

I would go with the 2-1-3 order. Try embedding another media player and allow a couple of months for them to fix the bug.