简体   繁体   English

Pygame 不会播放音频,但只能在作为守护进程运行时播放

[英]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.一点背景知识:我有一个运行 Raspbian(Debian) 9.11 的 Raspberry Pi 3b。 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.这个原始的 Pi 运行一个 Python 3 脚本,该脚本从手持扫描仪捕获文本输入并将其发送到 MySQL 数据库,然后播放 wav 文件,以便员工扫描知道它是成功的。 I daemonized this process so it was easier to restart (sometimes the scanners lose sync with their USB dongles).我守护了这个过程,所以它更容易重新启动(有时扫描仪与他们的 USB 加密狗失去同步)。 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).我不得不为我被要求创建的新实例稍微更改流程,所以我将 Raspbian 10.4(最新版本)加载到另一个 Pi 3b 上,并通过现在插入到本地 Maria DB(MySQL 基本上是)。 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.真正奇怪的是,当我在 Thonny 中加载它时,它可以很好地播放文件。 There's nothing in the syslog indicating Python or Pygame are generating any errors.系统日志中没有任何内容表明 Python 或 Pygame 正在生成任何错误。

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).我猜这与从 Raspberry Pi OS 转向 Linux 内核 5.4(旧的 Pi 运行内核 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我使用 pip 升级到 Pygame 1.9.6,它在停止或重新启动服务时将此错误添加到系统日志中

Sep 3 08:40:46 raspberrypi kernel: [164439.999815] bcm2835_audio bcm2835_audio: failed to close VCHI service connection (status=1) 9 月 3 日 08:40:46 raspberrypi 内核:[164439.999815] bcm2835_audio bcm2835_audio:未能关闭 VCHI 服务连接(状态 = 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). Kingsley 在评论中询问它在哪个用户下运行,答案是 root(我从未在系统服务文件中指定用户)。 I noted, however, that Thonny runs under the "pi" user.但是,我注意到 Thonny 在“pi”用户下运行。 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.到我的服务文件,现在 Pygame 音频再次在服务中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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