How to play audio with PyQt?

6

Is there any way to run an audio file with PyQt?

Is it possible to do this only with PyQt or do you need to install another library?

    
asked by anonymous 11.11.2016 / 13:48

2 answers

6

If you want to use the native method of PyQt, you have QSound :

meusom = QSound("alerta.wav")
meusom.play()
  

link

There is a method that returns a bool value, so you know if the feature is available:

bool QSound::isAvailable()

This is useful for you to check if you need an alternative notification medium if the platform is unable to play the audio.


The question speaks in PyQt 4, but for those who are using 5, it pays a glance at QSoundEffect also, more versatile and low latency:

  

link

    
11.11.2016 / 13:55
4

Apparently, QMedia is only available in PyQt5 (since% requires% requires Qt5), but Python itself can do some things without needing PyQt.

There is the playsound lib, which has no dependencies, to install via QMedia :

$ pip install playsound

And call it like this:

from playsound import playsound

playsound('/home/pasta/play.mp3')
    
11.11.2016 / 13:53