Python / Pygame按钮推动声音

时间:2016-07-06 02:10:20

标签: python audio raspberry-pi pygame

我是编程新手。我正在建立一个项目:当我按下我的门铃按钮时,一张图片会发送我的手机(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.5mm插孔发出声音的代码。我所拥有的编码(不起作用)是这样的:

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()

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

  

文件" / home / pi /桌面/声音code.py",第23行,in       mixer.music.play(sndA)TypeError:需要一个整数

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

我现在已经在最后一部分工作了大约4天了,而且我正处在一个时间线上,所以我只是在寻找任何帮助。

2 个答案:

答案 0 :(得分:2)

无论输入是什么,

mixer.music.load()都会返回None(请参阅文档here)。这意味着sndA也会获得None

pygame.mixer.music.play()方法需要两个数字(实际上是可选的,因此您不需要指定它们),因为您可以看到here

您不必使用任何变量来保存声音。只需拨打play(),即可播放以前加载的文件:

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

# ...

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

答案 1 :(得分:0)

尝试使用调音台中的Sound对象而不是音乐功能。

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

查看此链接: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/base/Optional.html

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

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