【问题标题】:Python/Pygame button push for soundPython/Pygame 按钮按下声音
【发布时间】:2016-07-06 20:22:30
【问题描述】:

我对编程很陌生。我正在构建一个项目:当我按下门铃按钮时,一张图片会发送到我的手机(带有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()

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

文件“/home/pi/Desktop/sound code.py”,第 23 行,在 Mixer.music.play(sndA) TypeError: an integer is required

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

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

【问题讨论】:

    标签: python audio raspberry-pi pygame


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      尝试使用混音器中的 Sound 对象而不是音乐函数。

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

      查看此链接: Pygame Sound object

      至于组合代码,我建议将发送图片的代码打包成一个函数,并在第二个的 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)
      

      【讨论】:

      • 我不得不说这是更漂亮的做法
      • 谢谢!这很有帮助,我明白了:)
      猜你喜欢
      • 1970-01-01
      • 2012-06-24
      • 2020-03-29
      • 1970-01-01
      • 2011-12-27
      • 2013-02-17
      • 2022-01-10
      相关资源
      最近更新 更多