Audio not working when selecting program from Projects folder

Rpi 4B 8Gb, Pitop4 DIY, Pitop OS

Hi there

I Wrote a simple program to play a sound file when motion is detected using a PIR sensor.
Its running fine when run on Mu and online through Further (load your own code).

But once I load the program to the Project.cfg dir, there is no audio.

I want to be able to run the project by selecting it from the Projects list.

I tried internal speaker, headphones, external speaker on the AV Jack, Bluetooth speaker. All of them is playing the sound when run on Mu and online on Further, but no audio when loading the program to the pitop and selecting the program from the projects list on either of them.

Code below:

When motion is detected, play sound file

from gpiozero import MotionSensor
from time import sleep
from pitop import Pitop
import pygame
path = “/home/pi/sounds/robot_sounds_named/”
sound_files =[“gobble.mp3”]

pygame.mixer.init()
speaker_volume = 1
pygame.mixer.music.set_volume(speaker_volume)
for sound_file in sound_files:
pygame.mixer.music.load(path + sound_file)

Define PIR

pir = MotionSensor (4)

initialise the Mini Screen, set the frames-per-second rate and font size

mini_screen = Pitop().miniscreen

while True:
pir.wait_for_motion()
print(“motion detected”)
mini_screen.display_multiline_text(“Motion Detected”, font_size = 20)
pygame.mixer.music.play()
pir.wait_for_no_motion()
print(“Motion Stopped!!”)
mini_screen.display_multiline_text(“No Motion”, font_size = 20)

Any help would be appreciated

Did you ever solve your audio problem?

I suspected the Pitop OS had interference with the audio when running the program locally.

I ended up running the latest Raspbian and installed all the Pitop drivers. This allowed me to still have access to the LCD screen and buttons, and the program worked. Lost some functionality though.

I can stop and start the program using the button selection but cannot swop between different programs. I wrote a script to start the program automatically after restart. (This I also could not do using Pitop Os.)

Anyhow for my project requirements this was enough

Can any staff comment on this?
@angus @duwudi @pi-topJorge @pi-topMIKE

hi @xerox2k4 and @Andre007 ;

Playing sounds using the Projects Menu from the miniscreen app worked for me; maybe the folders are not correctly configured or the path to the sound file isn’t correct?

This is what I used:

  • Projects folder is in /home/pi/Desktop/Projects/alarm
    You can create it in a terminal running mkdir -p /home/pi/Desktop/Projects/alarm

  • Inside this folder there should be 2 files:

    • project.cfg
    • alarm.py

The content of project.cfg is:

[project]
title=alarm
start=python3 alarm.py

The content of alarm.py is:

#!/usr/bin/env python3
from pathlib import Path
from time import sleep

import pygame
from pitop import Pitop

soundfile = Path("/home/pi/alarm.mp3")
if not soundfile.exists():
    print(f"Sound file {soundfile} does not exist")
    exit(1)

pygame.mixer.init()
speaker_volume = 1
pygame.mixer.music.set_volume(speaker_volume)
pygame.mixer.music.load(str(soundfile))

miniscreen = Pitop().miniscreen

while True:
    miniscreen.display_multiline_text("Play!", font_size = 20)
    pygame.mixer.music.play()
    sleep(2)
    miniscreen.display_multiline_text("Stopping", font_size = 20)
    pygame.mixer.music.stop()
    sleep(2)

Please note that I’m referencing a mp3 file that should be in /home/pi/alarm.mp3

After creating these files, using the miniscreen menu, navigate to ‘Projects’; at the top a ‘My Projects’ section should show up; clicking the ‘O’ button on it will take you to a new screen with a list of your projects; in this case only ‘alarm’ should be displayed. If you click on it, a new screen will let you run it, view some logs or delete it.