繁体   English   中英

Python / Pygame按钮按下以发出声音

[英]Python/Pygame button push for sound

我对编程非常陌生。 我正在建立一个项目:按下门铃按钮时,有图片发送我的电话(带有twilioImgur ),并且我还希望在按下同一按钮时门铃声音消失。 我最初的编码工作正常,图片正在发送到我的手机

import os.path as pth
import os
import re
import pyimgur
import time
import picamera
import RPi.GPIO as GPIO
from twilio.rest import TwilioRestClient

# Defining GPIO port on RPI
BUTTON = 19 

# setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position for a pushbutton)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Twilio credentials 
TWILIO_SID = "####"
TWILIO_AUTH = "####"

# Phone Numbers
HOME_PHONE = "####"
TWILIO_PHONE = "####"

# text message to send with photo
TXT = "Someones at the Door!"

# directory to save the snapshot in
IMAGE_STORAGE = "/home/pi/Pictures/"

# imgur client setup
IMGUR_ID = "#####"

# name and dimensions of snapshot image
IMG = "snaps.jpg"
IMG_WIDTH = 800
IMG_HEIGHT = 600

# initalize the Twilio client
client = TwilioRestClient(TWILIO_SID, TWILIO_AUTH)

# initialize imgur client
im = pyimgur.Imgur(IMGUR_ID)


try:


    # indefinite loop for the doorbell
    while True:

        GPIO.wait_for_edge(BUTTON, GPIO.RISING)
        print("DoorBell\n")
        with picamera.PiCamera() as camera:
            camera.resolution = (IMG_WIDTH, IMG_HEIGHT)
            camera.capture(IMAGE_STORAGE + IMG)  

        uploaded_image = im.upload_image(IMAGE_STORAGE + IMG, title=TXT)
        client.messages.create(
            to=HOME_PHONE,
            from_=TWILIO_PHONE,
            body=TXT,
            media_url=uploaded_image.link,
        )
finally:
    GPIO.cleanup() # ensures a clean exit

该代码可以很好地将图片发送到手机,现在我需要的是使按钮也可以通过RPI上的3.5毫米插孔发出声音的代码。 我对此的编码(不起作用)是这样的:

from pygame import mixer
import RPi.GPIO as GPIO
from time import sleep
from sys import exit

# Defining GPIO port on RPI
BUTTON = 19 

# setup GPIO using Broadcom SOC channel numbering
GPIO.setmode(GPIO.BCM)

# set to pull-up (normally closed position for a pushbutton)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)

mixer.init(48000, -16, 1, 1024)

sndA = mixer.music.load('/home/pi/Desktop/doorbell-7.mp3')

while True:
    try:
        if (GPIO.input(19) == True ):
            mixer.music.play(sndA)
            sleep(.01)
    except KeyboardInterrupt:
        exit()

当我尝试运行它时,我得到:

在Mixer.music.play(sndA)中,文件“ / home / pi / Desktop / sound code.py”第23行,TypeError:必须为整数

我想知道是否有人知道如何解决此问题,是否有办法将这两个脚本组合成一个?

我已经在最后一部分进行了大约4天的讨论,而且我处于时间表上,所以我只是在寻求任何帮助。

不管输入是什么, mixer.music.load()返回None (请参阅此处的文档)。 这意味着sndA也将获得None

但是pygame.mixer.music.play()方法需要两个数字(实际上是可选的,因此您无需指定它们)就可以在这里看到。

您不必使用任何变量来保存声音。 只需调用play()先前加载的文件:

mixer.music.load('/home/pi/Desktop/doorbell-7.mp3')

# ...

mixer.music.play(-1) # -1 = infinite loop

尝试使用混音器中的Sound对象代替音乐功能。

doorbell = pygame.mixer.Sound(filename)
doorbell.play()

出此链接: Pygame声音对象

至于组合代码,我建议将发送图片的代码打包成一个函数,然后在第二个if语句中调用它。 但是,对于循环的多次迭代,keypress函数将返回true,您可以通过存储先前的keypress值并将其与当前值进行比较来进行舍入:

last_keypress = False
while True:
    if (not last_keypress) and (GPIO.Input(19)):
        <do stuff>
    last_keypress = GPIO.Input(19)
    time.sleep(.01)

暂无
暂无

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

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