February 25, 2024, 18:00
pygame.init() soundfile = pygame.mixer.Sound('./redfr/Bonjour.wav') soundfile.play()This way it takes a very long time for the script to start and then I get this
pygame 2.5.2 (SDL 2.0.14, Python 3.9.2) Hello from the pygame community. https://www.pygame.org/contribute.html Using device: speaker-phatand it plays the sound, and if I press the button it plays the sound instantly. great right ? well not really because right after the sound played the program crashed saying Segmentation fault :/ Does anyone has an idea of what's wrong here ?
python #import time import RPi.GPIO as GPIO #import playsound import os import random #import subprocess import pygame # button stuff GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) buttonPin = 3 GPIO.setup(buttonPin, GPIO.IN, GPIO.PUD_UP) #listng sounds in folder path="./redfr/" files=os.listdir(path) #using os.system os.system("aplay ./redfr/Bonjour.wav &") #using playsound playsound('./redfr/Bonjour.wav') #using pygame pygame.init() soundfile = pygame.mixer.Sound('./redfr/Bonjour.wav') soundfile.play() def button_callback(channel): print("Button was pushed!") randomfile = random.choice(files) file = path + randomfile #using os.system os.system('aplay ' + file + ' &') #using playsound playsound(file) #using pygame soundfile = pygame.mixer.Sound(file) soundfile.play() GPIO.add_event_detect(buttonPin,GPIO.FALLING,callback=button_callback) # Setup event on button rising edge message = input("Press enter to quit\n\n") # Run until someone presses enter GPIO.cleanup() # Clean up
playsound is relying on another python subprocess. Please use pip install pygobject if you want playsound to run more efficiently. Traceback (most recent call last): File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 261, in <module> playsound(argv[1]) File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 162, in _playsoundNix import gi File "/home/bozarre/.local/lib/python3.9/site-packages/gi/__init__.py", line 40, in <module> from . import _gi ImportError: libgirepository-1.0.so.1: cannot open shared object file: No such file or directory Traceback (most recent call last): File "/home/bozarre/button.py", line 23, in <module> playsound('./redfr/Bonjour.wav') File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 254, in <lambda> playsound = lambda sound, block = True: _playsoundAnotherPython('/usr/bin/python3', sound, block, macOS = False) File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 229, in _playsoundAnotherPython t.join() File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 218, in join raise self.exc File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 211, in run self.ret = self._target(self._args, *self._kwargs) File "/home/bozarre/.local/lib/python3.9/site-packages/playsound.py", line 226, in <lambda> t = PropogatingThread(target = lambda: check_call([otherPython, playsoundPath, _handlePathOSX(sound) if macOS else sound])) File "/usr/lib/python3.9/subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', '/home/bozarre/.local/lib/python3.9/site-packages/playsound.py', './redfr/Bonjour.wav']' returned non-zero exit status 1.