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
yes, the mp3 file is in the same folder
I'm using headphones instead of speakers. Wonder if that may have something to do with it? All other video and audio files play through the headphones though.

Ok, I can hear wav files. Maybe I don't have the right codecs.
You must set QMediaContent and use the full path

from PySide2.QtCore import QUrl
from PySide2.QtMultimedia import QMediaPlayer, QMediaContent
import PySide2.QtWidgets as QtWidgets
import sys
 
 
app = QtWidgets.QApplication(sys.argv)  # Does all the setup work required to use Qt
player = QMediaPlayer()
media_content = QMediaContent(QUrl.fromLocalFile('/tmp/test.mp3')) # use full path here
player.setMedia(media_content)
player.setVolume(70)
player.play()
sys.exit(app.exec_())  # Stops program from exiting
Still not hearing sound. I will hook my speakers up in a little while and see if it's because I'm using headphones.
I can hear wav files. Maybe I don't have the right codec to play mp3 files.
Do you have gstreamer plugins and libqt5multimedia5-plugins ?

find out with

dpkg -l libqt5multimedia5-plugins

dpkg -l | grep gstreamer
Quote:Do you have gstreamer plugins and libqt5multimedia5-plugins ?

find out with

dpkg -l libqt5multimedia5-plugins

dpkg -l | grep gstreamer

Yes, I have those installed on my linux os. As with the windows os, I can here the wav files. Just cant get the mp3 files to work.
The solution was having to check the player state. so I did
from PySide2.QtCore import QUrl, QDir
from PySide2.QtMultimedia import QMediaPlayer, QMediaContent
import PySide2.QtWidgets as QtWidgets
import sys


app = QtWidgets.QApplication(sys.argv)  # Does all the setup work required to use Qt
player = QMediaPlayer()
file = 'here.mp3'
dir = QDir()

media_content = QMediaContent(QUrl.fromLocalFile(f'{dir.currentPath()}/{file}')) # use full path here
print(player.state())
player.setMedia(media_content)
player.setVolume(70)
if player.state() == player.StoppedState: # Check if player state is stopped
    player.play()
sys.exit(app.exec_())  # Stops program from exiting
This also works. Guess I just has an error in my coding.
from PySide2.QtCore import QUrl, QDir
from PySide2.QtMultimedia import QMediaPlayer, QMediaContent, QMediaPlaylist
import PySide2.QtWidgets as QtWidgets
import sys


app = QtWidgets.QApplication(sys.argv)  # Does all the setup work required to use Qt
player = QMediaPlayer()
path = QDir().currentPath()
url = QUrl()
file = url.fromLocalFile(f'{path}/here.mp3')
playlist = QMediaPlaylist(player)
playlist.addMedia(QMediaContent(file))
player.setPlaylist(playlist)
player.play()
sys.exit(app.exec_())  # Stops program from exiting
Any idea how to view the playlist?
Pages: 1 2