简体   繁体   English

Python 脚本不会像 cgi 一样播放声音,而是通过命令行播放

[英]Python script won't play sound as cgi, but via command line

The CGI python script shown below, sound.py, plays a sound through a speaker connected to a Raspberry's audio output.下面显示的 CGI python 脚本 sound.py 通过连接到 Raspberry 音频 output 的扬声器播放声音。 The sound plays fine when I execute the script from the command line via “python sound.py”.当我通过“python sound.py”从命令行执行脚本时,声音播放良好。 However, when I try to trigger it as a cgi script by invoking it via the Web ( http://192.168.1.246/cgi-bin/sound.py ), the sound won't play, and the browser will only display “Hi Hello World,” (which, I suppose. indicates that the server at least recognizes it as a CGI script and executes it without producing errors).但是,当我尝试通过 Web ( http://192.168.1.246/cgi-bin/sound.py ) 调用它作为 cgi 脚本触发它时,声音不会播放,浏览器只会显示“嗨,Hello World,”(我想这表明服务器至少将其识别为 CGI 脚本并执行它而不会产生错误)。

I thought that the issue might have to do with the permissions/ownership of the sound file and tinkered around with those, but that didn't help (I'm also pasting below information about the ownership and permission of the script itself, and the sound file).我认为这个问题可能与声音文件的权限/所有权有关,并对其进行了修改,但这并没有帮助(我还在下面粘贴了有关脚本本身的所有权和权限的信息,以及声音文件)。

Thanks a lot for helping!非常感谢您的帮助!

Marc.马克。

-rwxrwxrwx 1 root root 441 Jul 10 23:23 sound.py -rwxr-xr-x 1 root root 29812 Jul 9 22:58 cardinal-short.mp3 -rwxrwxrwx 1 根 441 7 月 10 日 23:23 sound.py -rwxr-xr-x 1 根 29812 7 月 9 日 22:58 cardinal-short.mp3

Script (sound.py):脚本(sound.py):

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()

import pygame

name = "cardinal-short"
pygame.mixer.init()
pygame.mixer.music.load("/var/www/html/cgi-bin/cardinal-short.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(1.0)
while (pygame.mixer.music.get_busy() == True):
  continue
pygame.mixer.quit()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hi Hello World!"

I was able to fix this via:我能够通过以下方式解决此问题:

sudo usermod -aG audio www-data

and then restarting apache.然后重启 apache。

The problem was that, and I quote from elsewhere on the web:问题是,我从 web 的其他地方引用:

the sound devices (in /dev/snd/) are only accessible to members of group "audio".声音设备(在 /dev/snd/ 中)只能由“音频”组的成员访问。 pi is a member of that group, and www-data is not. pi 是该组的成员,而 www-data 不是。

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

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