gravetii gravetii - 1 year ago 53
Python Question

Why does QtGui.QSound just not play anything at all?

So, I am trying to play a sound in my application. Typically the sound needs to be played after a the end of a countdown and then user-interaction is stopped.

But, the problem is

QSound
just ends up playing nothing at all. I have tried mp3, wav etc. without any result.

What could be the possible reason?

I've tried this both on Linux and Windows. Here's what I tried (both play and play2 functions):

class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.initUI()

def initUI(self):
self.setGeometry(300, 300, 200, 200)
self.btn = QtGui.QPushButton("Play", self)
self.btn.clicked.connect(self.play)
self.btn.move(50, 80)

def play(self):
c = QtGui.QSound("1.mp3")
c.play()

def play2(self):
QtGui.QSound.play("1.mp3")


Neither of them work!

EDIT: I used Phonon for my purpose, but I have no idea why this doesn't work.

Answer Source

You should call QSound.isAvailable() to check whether the necessary sound facilities are available on your platform (but note that this may produce a false positive on Windows).

When I try this on Linux, is prints False, even though phonon is installed and I can play audio using the PyQt Phonon module.

This may be because I don't have NAS support built in (I haven't checked), which is the key requirement that determines availability on X11. For other platforms, see the Detailed Description for QSound - in particular, note that QSound will only play WAV files on Windows.