Is it just me or is QMediaplayer not working properly?

Heads up! You've already completed this tutorial.

cmaarten15855 | 2020-08-08 09:39:34 UTC | #1

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!


martin | 2020-08-08 20:57:55 UTC | #2

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.

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

cmaarten15855 | 2020-08-08 21:01:20 UTC | #3

[quote="martin, post:2, topic:401"] ctShow 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 [/quote]

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


MrKartofel | 2021-02-02 19:09:40 UTC | #4

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 :

python
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().__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_()

The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Is it just me or is QMediaplayer not working properly? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.