gravetii gravetii - 1 year ago 91
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

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):

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

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

def play2(self):"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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download