Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to temporarily empty a QMediaPlayer object?



  • Hello

    I've got a QMediaPlayer in my PyQt5-based app and I've got a situation where I want to close the current video (so it goes to black, or equivalent), wait for the user to select another video, then open it.

    The pseudocode is:

    window.mediaPlayer.setMedia(QMediaContent(QUrl(videoFile1)))
    wait_for_user_to_say_they_are_done_with_videoFile1()
    nullMedia = QMediaContent(None)
    window.mediaPlayer.setMedia(nullMedia)
    user_selects_videoFile2()
    window.mediaPlayer.setMedia(QMediaContent(QUrl(videoFile2)))
    

    I'm having a problem with setting nullMedia, i.e. makng the QMediaPlayer show black. If I run (the equivalent of) the above code, then the video panel does go black. However, when I open videoFile2, the video panel stays black. videoFile2 doesn't get displayed properly.

    If I comment out the 'setMedia(nullMedia)' line, then videoFile1 remains visible, and then when videoFile2 is selected, it loads absolutely fine. So something about the setting to nullMedia is causing the QMediaPlayer to fail on future setMedia() calls.

    Please can somebody tell me what I'm doing wrong here? What's the proper way to make a QMediaPlayer object empty so it no longer contains a video file, without affecting its ability to load the next file?


  • Lifetime Qt Champion

    @donquibeats Maybe you should call https://doc.qt.io/qt-5/qmediaplaylist.html#next on the playlist after loading videoFile2?
    Or call https://doc.qt.io/qt-5/qmediaplaylist.html#clear before setting videoFile2?



  • Thanks @jsulm for pointing me in the right direction.

    This is what worked for me:

    emptyPlaylist = QMediaPlaylist()
    window.mediaPlayer.setPlaylist(emptyPlaylist)
    

Log in to reply