Python Forum

Full Version: PySide2 and QMediaPlayer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I've been trying to get mp3 files to play using QMediaPlayer with no success. Can anyone shed some light on what I'm missing?
The basic code. If you wish to see all the code, let me know.
According to the examples this works. I get no sounds or errors.
Thanks.

#! /usr/bin/env python3

from PySide2.QtCore import QUrl
from PySide2.QtMultimedia import QMediaPlayer, QAudio

player = QMediaPlayer()
player.setMedia(QUrl.fromLocalFile('My Music/here.mp3'))
player.setVolume(70)
player.play()
I think, not sure of this, pyside2 was written for python 2.7.
There is a pyside6 version available pip install PySide6
see: https://pypi.org/project/PySide6/
What is your OS ?
doc page for pyside6: https://doc.qt.io/qtforpython/

Show your OS for Axel_Erfurt, he may have an example for you.
os is windows 10
Just because all you want to do is play a mp3 file it doesn't mean you can skip the setup work of making an application or running it.
from PySide2.QtCore import QUrl
from PySide2.QtMultimedia import QMediaPlayer, QAudio
import PySide2.QtWidgets as QtWidgets
import sys
 
app = QtWidgets.QApplication(sys.argv)  # Does all the setup work required to use Qt
player = QMediaPlayer()
player.setMedia(QUrl.fromLocalFile('Hotel California.mp3'))
player.setVolume(70)
player.play()
sys.exit(app.exec_())  # Stops program from exiting
This example will not work for PySide6 which has several changes to the media player.

Now stop bothering me. I'm chillin to the Eagles
Thanks for the example deanhystad but, still not hearing anything on windows 10 os. I'm going to give it a try on my linux os.
I ran the program on My Windows 10 machine and it works. Are you sure about the file location? Should get an error if that happens. Have you verified that the mp3 file is playable?
The file is playable. On my linux did get three errors.
GStreamer; Unable to pause - "file:here.mp3"
GStreamer; Unable to play - "file:here.mp3"
Error: "Could not open resource for reading."

Maybe a path problem?
Is 'Hotel California.mp3' in the script folder?

If not try the full path to 'Hotel California.mp3'
Pages: 1 2