Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem playing audio with playsound on python3

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
like image 649
Pa Ra Avatar asked Jun 16 '19 16:06

Pa Ra


People also ask

Does Playsound work on Linux?

playsound module Implementation is different on platforms. It uses windll. winm on Windows, AppKit. NSSound on Apple OS X and GStreamer on Linux.

How do you stop music playing in Playsound Python?

After successful execution of the program, audio file will start playing. Now Click on terminal, once you see the cursor, type Ctrl+C , it will bring you out of the terminal and audio will also stop playing.

How do I install Python Playsound?

https://pypi.python.org/pypi/playsound/1.2.1 - link for playsound module it is very easy to download. You can download it using pip by navigating with cmd to your python folder (usualy C:\Users\UserName\AppData\Local\Programs\Python\Python35-32) and writing: python -m pip install playsound (if you are using python 3).


1 Answers

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
like image 51
Pa Ra Avatar answered Oct 19 '22 10:10

Pa Ra