I am currently trying to run a PYQT5 GUI on my RPI 4B using PYQT5 and its QtMultimedia. I have 2 speakers (running through an amp) connected through the 3.5mm jack (BMC2835 headphones), and while playing audio from the VLC player, it has no issue and plays audio just fine.
However, when running my own code, I receive no output on the 3.5mm jack. There are no errors in the cml window. I have set it as default running amixer cset numid=3 1
I got pulseaudio, pavucontrol, and alsa installed and I can see the that none of them are muted. When running lsmod I find my headphone (bmc2835) and it also says that its default audio device.
The part of my code that relates to the audio playing:
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtMultimedia
self.pushButton.clicked.connect(self.playaudio)
def playaudio(self):
sound_file="Fehler.wav"
sound=QtMultimedia.QSoundEffect()
sound.setSource(QtCore.QUrl.fromLocalFile(sound_file))
#the sound_file is in the same folder as the file that is being run
#sound.setLoopCount(QtMultimedia.QSoundEffect.Infinite)
sound.setVolume(75.0)
sound.play()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The programme I have executes just fine, and I can run it without any options. When I click the button where I have my def function written, it shows the click, but no audio is played, nor is there any errors in the terminal.
I am of no clue how it can be so difficult to make it play an audio file :(
**EDIT I already followed https://forum.qt.io/topic/93033/no-sound-from-raspi3-3-5jack-using-qtmediaplayer/3 http://blog.scphillips.com/posts/2013/01/sound-configuration-on-raspberry-pi-with-alsa/
When trying this link, https://stackoverflow.com/questions/36660694/qt-no-audio-output-device-yocto-poky , and i try aplay Fehler.wav (my audio file), i can see that it plays but without audio?!
**EDIT 2: Fiddling around with aplay, I have been able to determine that I have 2 hardware sound cards (Card 0 and Card 1) with 4 subdevices on each (By running aplay -l). With this I tested further, and I have now identified that the HDMI speakers are running on soundcard hw0,0 (Card 0, subdevice 0) and my attached speakers (through the bmc2835) on hw1,0 (Card 1, subdevice 0)).
Is there any way I can write my pyqt code to select the audio output I want (hw1,0)?
**EDIT 3: I think I have found the solution in case any of you reading this are experiencing the same: By opening the pavucontrol panel > I made sure that the output of the headphones were selected by default (marking it so it becomes pressed), thereafter I updated my QT code where I added a self.sound (my variable) and matched it with an instance (I call it in the class instead of in my function to avoid several instances being made) > self.sound=QtMultimedia.QSoundEffect(QtCore.QCoreApplication.instance())
Changed from sound=QtMultimedia.QSoundEffect()
My functioning code looks like:
self.pushButton.clicked.connect(self.playaudio)
self.sound=QtMultimedia.QSoundEffect(QtCore.QCoreApplication.instance())
def playaudio(self):
sound_file="Fehler.wav"
self.sound.setSource(QtCore.QUrl.fromLocalFile(sound_file))
#sound.setLoopCount(QtMultimedia.QSoundEffect.Infinite)
self.sound.setVolume(90)
self.sound.play()