java按键音效,按下按钮停止声音循环

我的 RPi 附有 IOPi 板 . 当按下连接到I / O引脚的按钮时,我试图让它发出声音 .

这是通过课程实现的,唯一的问题是当按下按钮时声音会不断播放 . 代码如下所示:

from ABElectronics_IOPi import IOPI

from time import sleep

import pygame

from inc import *

pygame.init()

global bus

InputPinNumber1 = 1

InputPinNumber2 = 1

bus1= IOPI(0x21)

bus2= IOPI(0x20)

while InputPinNumber1 <=34:

bus1.setPinDirection(InputPinNumber1, 1)

bus1.setPinPullup(InputPinNumber1, 1)

bus1.invertPin(InputPinNumber1, 1)

InputPinNumber1 +=1

if InputPinNumber1 == 34:

print("Bank 1 Ready ")

break

while InputPinNumber2 <=34:

bus2.setPinDirection(InputPinNumber2, 1)

bus2.setPinPullup(InputPinNumber2, 1)

bus2.invertPin(InputPinNumber2, 1)

InputPinNumber2 +=1

if InputPinNumber2 == 34:

print("Bank 2 Ready ")

break

class layout(object):

def __init__(self, switch, bank, sound):

self.switch=switch

self.bank=bank

self.sound=sound

self.pre=0

def active(self):

input1 = bus1.readPin(self.switch)

input2 = bus2.readPin(self.switch)

if(self.pre != input1) and (self.bank ==0):

print("Button Press at pin " + str(self.switch))

print("Switch " + str(self.switch))

print("Bank " + str(self.bank))

pygame.mixer.init()

pygame.mixer.music.load(self.sound)

pygame.mixer.music.play()

sleep(3)

if(self.pre != input2) and (self.bank ==1):

print("Button Press at pin " + str(self.switch))

print("Switch " + str(self.switch))

print("Bank " + str(self.bank))

pygame.mixer.init()

pygame.mixer.music.load(self.sound)

pygame.mixer.music.play()

sleep(3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值