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

QMediaplayer incorrect duration : how to set windowsmediafoundation as preferred plugin?



  • I was playing around with QMediaplayer, trying to create a music player with PyQt (5.9.2) and noticed that the player duration is incorrect on files with "unusual" bitrates (e.g. 241 kbits/s). I found a similar question here https://forum.learnpyqt.com/t/is-it-just-me-or-is-qmediaplayer-not-working-properly/401 and the answer suggests that the Direct Show back-end is causing this problem and recommend switching to Windows media foundation by running the following code:

    import os
    os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation'
    

    The problem is that I just can't manage to switch to windowsmediafoundation when running this code. I tried to insert it before calling QApplication() but nothing seems to be happening. See below an example of how I tried to implement the code :

    import os
    # os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here
    
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtMultimedia import *
    import sys
    
    # os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here
    
    class MainWindow(QMainWindow):
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
    
            self.player = QMediaPlayer()
            self.player.durationChanged.connect(self.print_duration)
            self.player.setMedia(QMediaContent(QUrl.fromLocalFile(path_to_file)))
            self.show()
    
        def print_duration(self, duration):
            print(duration)
    
    if __name__ == "__main__":
    
        os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' #This is where I tried to run it first
    
        app = QApplication(sys.argv)
    
        # os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation' I also tried to run it here
    
        window = MainWindow()
        app.exec_()
    

    Indeed the duration bug is still present. Furthermore, when I go into the Library\plugins\mediaservice directory of my Anaconda installation, both the dsengine.dll and wmfengine.dll are present and trying to delete the dsengine.dll while running my music player app results in an error because dsengine.dll is being used by Python. wmfengine.dll on the other hand, can be deleted without such error. Hence, Direct Show is still being preferred by QtMultimedia.

    It could be that I'm not using QT_MULTIMEDIA_PREFERRED_PLUGINS correctly.

    My OS is Windows 10 and my Python version is 3.9.1.


Log in to reply