QMediaplayer incorrect duration : how to switch mediaservice backend ?

MisterKartofel teanh1kns at gmail.com
Wed Feb 3 10:35:58 GMT 2021


I recently started using QMediaplayer 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
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()


    def print_duration(self, duration):

if __name__ == "__main__":

'windowsmediafoundation' #This is where I tried to run it first

    app = QApplication(sys.argv)

'windowsmediafoundation' I also tried to run it here

    window = MainWindow()

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,
how can I switch the mediaservice to wmf?

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

Thank you in advance for your help.

Best regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210203/8d5883dd/attachment.htm>

More information about the PyQt mailing list