Is it just me or is QMediaplayer not working properly?

I started to work on a mediaplayer a while ago and started learning PyQT and all that is going good. But I noticed that when you are using QMediaplayer the .duration() function is completly wrong all mp3 files that are loaded give a time that is way longer then it is supposed to be. I also noticed that not all audio files are loaded or even working with the Qmediaplayer while other python modules work fine with them. To give you an example go and look at the failamp mediaplayer that you can find on this site. I really hope this gets fixed because now I’m forced to use python VLC module or other audio modules in python and it’s not an easy task to implement it because of the poor documentation there is. Besides this issue PyQt is really good for making GUI’s in python hope to hear something about this issue soon!

It’s not just you, there is something very odd with Qt Mediaplayer on Windows. But I just had a look into it and there seems to be a solution.

In the Qt documentation on Windows multimedia it mentions that there are two plugins that can be used on Windows – directshow or windowsmediafoundation.

The DirectShow backend is the legacy one, but seems for some reason to be selected by default (documentation suggests it could be because of the need for a camera backend). If you switch to windowsmediafoundation instead, you get accurate duration reporting and better media file support.

To enable it, just add the following to your application, somewhere before you create the QApplication instance.

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

Thank you! It works way better now, to bad I didn’t ask it sooner!

I face the same problem but I’m not able to fix it. Could you please provide a simple example of how to implement the os.environ command ? I tried to use it in my own QMediaplayer but it does not seem to switch the plugin to wmf. Here is an example of how I tried to implement it :

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_()