简体   繁体   中英

Pygame won't play audio, but only when running as a daemon

A bit of background: I have a Raspberry Pi 3b running Raspbian(Debian) 9.11. This original Pi runs a Python 3 script that captures text input from a handheld scanner and sends it into a MySQL database, and then plays a wav file so the employees scanning know it was successful. I daemonized this process so it was easier to restart (sometimes the scanners lose sync with their USB dongles). The main line in the daemon file is this

ExecStart=/usr/bin/python3 /home/pi/Desktop/scanner.py

I had to change the process slightly for a new instance I was asked to create, so I loaded Raspbian 10.4 (latest) onto another Pi 3b and moved everything over with only the slight tweak that it now inserts to a local Maria DB (MySQL basically). Daemon runs just fine... except that no audio plays (but the DB insert still happens). What's really weird is when I load it in Thonny, it plays the file just fine. There's nothing in the syslog indicating Python or Pygame are generating any errors.

The relevant code here (abbreviated) is

import pygame.mixer
# sets up sound
pygame.mixer.init()
goodscan = pygame.mixer.Sound('/home/pi/Desktop/goodscan.wav')
duplicatescan = pygame.mixer.Sound('/home/pi/Desktop/duplicateScan.wav')
badscan = pygame.mixer.Sound('/home/pi/Desktop/badScan.wav')

#function that the USB listener calls
def scan(scanner):
    #some scanner processing codes here
    if prevScan == x:
        duplicatescan.play()
        print("Duplicate Scan")
        continue;
                 
     else:
        splits = x.split('-')
        if(len(splits) < 3):
            print("No prefix provided")
            #write to error log
            badscan.play();
            continue;
        prefix = splits[0][0] + splits[0][1]
        po = splits[1] + '-' + splits[2]
        scanner_number = splits[0][2] + splits[0][3]
        polist.extend([po, prefixes[prefix], scanner_number])
        goodscan.play()
        
        #DB insert (this always works)                
        scans = {
           "timestamp": time_str,
           "ponum": x,
           "status": polist[1],
           "scanner": polist[2]
        }
        statusUpdate.databaseUpdate(polist)

How can I debug why this isn't working in daemon mode?

I'm guessing this is related to the shift from the Raspberry Pi OS to Linux kernel 5.4 (the older Pi is running kernel 4.19). I used pip to upgrade to Pygame 1.9.6, which added this error to the syslog when stopping or restarting the service

Sep 3 08:40:46 raspberrypi kernel: [164439.999815] bcm2835_audio bcm2835_audio: failed to close VCHI service connection (status=1)

Kingsley asked in a comment what user this runs under and the answer was root (I never specified a user in the system service file). I noted, however, that Thonny runs under the "pi" user. I figured giving that a shot might work, so I added

User=pi

to my service file and now Pygame audio works again in the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM