第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在播放媒體文件時(shí)添加結(jié)束 Python 循環(huán)

如何在播放媒體文件時(shí)添加結(jié)束 Python 循環(huán)

茅侃侃 2023-03-16 17:57:09
Python 新手,使用 Raspberry Pi、GPIO 和 pygame。如果在播放媒體文件時(shí)按下按鈕,我想結(jié)束循環(huán)?,F(xiàn)在我有了它,所以你按下按鈕,mp3 文件就會(huì)一直播放(這是一個(gè) 30 分鐘的剪輯)。我希望如此,如果再次按下按鈕,媒體會(huì)重置并從頭開(kāi)始播放。我嘗試添加一個(gè) break 和一個(gè) if 語(yǔ)句,但它只是忽略它,因?yàn)?mp3 文件已經(jīng)在播放。我該怎么做呢?這是我的代碼的樣子:while True:while GPIO.input(buttonPin) == GPIO.LOW:    if GPIO.input(buttonPin) == GPIO.LOW:        pygame.mixer.init()        pygame.mixer.music.load(open("audio.mp3"))        pygame.mixer.music.play()        while pygame.mixer.music.get_busy():            time.sleep(1)    else:        break
查看完整描述

1 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

嘗試這個(gè):


musicplay = False       #This variable will be used as a "toggle". When the button is pressed, it will flick from false to true, or true to false


def playbackmedia(): # Define function that is called when button pin is pressed

        

        global musicplay # musicplay is a global variable that is outside of the scope of function

        

        musicplay = not musicplay # Here we are toggling the music play 

        

        if musicplay: # if music play is true, play music

                pygame.mixer.init()

                pygame.mixer.music.load(open("audio.mp3"))

                pygame.mixer.music.play()

        else:  # if music play is false, stop the music

                pygame.mixer.stop()


def loop():

        

        #GPIO here add_event_detect will detect when the buttonPin has a falling edge (Just as the button is pressed)

        #The bounce time is to give you enough time for the signal to be read clearly. If this wasn't used, when you press the button,

        #because of mechanical vibration, the connections fluctuate and it could be read by the Pi as pressing the button really quickly over and over 

        #again until you let go of the button. You have 300 milliseconds to let go of the button before the playback media function is called

        

        GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=playbackmedia, bouncetime=300) 

        

        while True:

               pass

我建議您訪問(wèn) pygame 網(wǎng)站以獲取有關(guān)如何控制混音器的更多詳細(xì)信息。


https://www.pygame.org/docs/ref/mixer.html


我還建議您訪問(wèn)此網(wǎng)站以獲取有關(guān) GPIO.RPi 模塊的更多信息


https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/


查看完整回答
反對(duì) 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)