Problem playing audio with playsound on python3
Asked Answered
J

2

11

Testing on RaspberryPi3 B+ model and have just 2 lines of py code. Python version 3.5.3

from playsound import playsound 

 playsound("alarm.wav")

I get error below, even after installing packages gst-make, gstreamer-player, fisspy and pgi on Thonny IDE. Unsure what else is required. Is there an alternate package for sound to be emitted?

Traceback (most recent call last):
  File "sound.py", line 3, in <module>
    playsound("home/pi/alarm.wav")
  File "/home/pi/.local/lib/python3.5/site-packages/playsound.py", line 92, in _playsoundNix
    gi.require_version('Gst', '1.0')
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 118, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gst not available
Jurisprudent answered 16/6, 2019 at 16:46 Comment(1)
The preferredsoundplayer package solved my problem with playsound in raspbery pi 2Titania
J
11

Answering my own Q after digging through a lot of posts. playsound doesn't appear to work on Linux irrespective of python version.

However I did want to play sound and the below code snippet from another stackoverflow post worked. https://raspberrypi.stackexchange.com/questions/7088/playing-audio-files-with-python

import pygame
pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue
Jurisprudent answered 16/6, 2019 at 19:5 Comment(1)
actually playsound works with ubuntu, I have installed in my machine, but I'm trying for long hours to install it in an ubuntu using snapcraft and I gave up, I'll try pygame.Nonexistence
B
1

There is an alternate package for emitting sound.

pip install preferredsoundplayer

then

from preferredsoundplayer import playsound

For more information see the docs.

Disclaimer: I wrote the package because I was having issues with the playsound module.

Buhler answered 27/3, 2022 at 15:38 Comment(1)
This worked beautifully on Ubuntu 22.04 with Python 3.10.12. It's a great package, thanks.Fraudulent

© 2022 - 2024 — McMap. All rights reserved.