【发布时间】:2019-01-09 04:24:10
【问题描述】:
您好,我在 python 中创建了一个程序,当按下按钮时,它会播放音频文件并在 Raspberry Pi 上运行。我创建了一个网页,用户可以转到 Raspberry Pi 的 IP 地址并上传 MP3,然后他们可以选择他们想要播放的文件。有一些 PHP 代码查看目录以填写下拉列表。如何获取用户从下拉列表中选择的内容以导入 python 程序。这是 HTML/PHP 主页。
<html>
<body>
<h1> Welcome to the Audio Setup </h1>
<hr />
<h3> Use to add mp3 file to server: </h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select MP3 to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<br />
<br />
<input type="submit" value="Upload MP3" name="submit">
</form>
<br />
<br />
<h3> Select file to play:
<hr />
<form name="sound" method="post" enctype="multipart/form-data" action="/home/pi/PushButton.py">
<label for="file">Select:</label>
<select name="sound">
<option value=""> Select File</option>
<?php
$dirPath = dir('/var/www/html/uploads/');
$imgArray = array();
while (($file = $dirPath->read()) !== false) {
if ((substr($file, -3)=="mp3") || (substr($file, -3)=="MP3")) {
$imgArray[] = trim($file);
}
}
$dirPath->close();
sort($imgArray);
$c = count($imgArray);
for($i=0; $i<$c; $i++) {
echo "<option value=\"" . $imgArray[$i] . "\">" .
$imgArray[$i] . "\n";
}
?>
</select>
</form>
</body>
</html>
这是它要进入的python程序。
import os
import sys
from time import sleep
import RPi.GPIO as GPIO
import mutagen
import cgi
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.IN)
form = cgi.sound()
if form.getvalue() != null:
musicFile = form.getvalue()
timer = musicFile.info.length + 1
while True:
if GPIO.input(23) == False:
os.system("omxplayer -o local /var/www/html/uploads/" + musicFile)
sleep(timer);
我对编程还很陌生,从我研究的内容来看,cgi 是一种转移选择的方法,但可能不是最好的方法。如果这不是正确的方法,或者如果有更好的方法,请告诉我。
【问题讨论】: