[英]Pygame music switch (ON/OFF multiple songs)
我真的是python新手,所以我沒有太多經驗。 我正在創建一個RPi程序來使用撥動開關播放和停止7首不同的歌曲( https://ktechnics.com/wp-content/uploads/2015/12/mini-6a-125vac-spdt-mts-102-3- pin-2 position-on-on-on-toggle-switch.jpg )。
我正在使用GPIO引腳的上拉/下拉電阻器作為開關和pygame.mixer.music模塊來播放歌曲。 一首歌曲可以正常工作:
import RPi.GPIO as GPIO
import pygame
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
pygame.init()
pygame.mixer.init()
cancion_1 = '/media/pi/PIERRE/0001.mp3'
while True:
switch_1 = GPIO.input(18)
if switch_1 == False:
if pygame.mixer.music.get_busy() == False:
print("Cancion 1 Sonando")
pygame.mixer.music.load(cancion_1)
pygame.mixer.music.play()
else:
if pygame.mixer.music.get_busy() == True:
print("Cancion 1 Callada")
pygame.mixer.music.stop()
但是,當我嘗試添加更多歌曲時,該程序僅播放第一首歌曲:
import RPi.GPIO as GPIO
import pygame
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
pygame.init()
pygame.mixer.init()
cancion_1 = '/media/pi/PIERRE/0001.mp3'
cancion_2 = '/media/pi/PIERRE/0002.mp3'
while True:
switch_1 = GPIO.input(18)
switch_2 = GPIO.input(19)
if switch_1 == False:
if pygame.mixer.music.get_busy() == False:
print("Cancion 1 Sonando")
pygame.mixer.music.load(cancion_1)
pygame.mixer.music.play()
elif switch_1 == True:
if pygame.mixer.music.get_busy() == True:
print("Cancion 1 Callada")
pygame.mixer.music.stop()
elif switch_2 == False:
if pygame.mixer.music.get_busy() == False:
print("Cancion 2 Sonando")
pygame.mixer.music.load(cancion_2)
pygame.mixer.music.play()
elif switch_2 == True:
if pygame.mixer.music.get_busy() == True:
print("Cancion 2 Callada")
pygame.mixer.music.stop()
我嘗試為每首歌曲創建一個函數,然后再調用它,但是它也不起作用。 任何想法??
正如我在評論中提到的那樣,您的邏輯僅允許播放第一首歌曲。 因為第一個條件( if switch_1 == False:
或第二個條件( elif switch_1 == True:
將為true。 因此,永遠不會達到條件3和4。
這是一種重新配置的方法:
import RPi.GPIO as GPIO
import pygame
# Create a list of tuples. First index is gpio pin, 2nd is song
# This marries the pin data with the song data
# It also allows you to easily add more pins/songs
song_data = [(18, "/media/pi/PIERRE/0001.mp3"),
(19, "/media/pi/PIERRE/0002.mp3")]
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Use a loop to setup pins
for pin,_ in song_data:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
pygame.init()
pygame.mixer.init()
# A function to check a pin and react
def check_toggle(pin, song):
# Looks like you want to play when switch == False
# I'm adding these variables to clarify
button_is_pressed = not GPIO.input(pin)
music_is_playing = pygame.mixer.music.get_busy()
if button_is_pressed and not music_is_playing:
pygame.mixer.music.load(song)
pygame.mixer.music.play()
elif not button_is_pressed and music_is_playing:
pygame.mixer.music.stop()
while True:
for pin,song in song_data:
check_toggle(pin, song)
# You might want to add a sleep here to keep from hogging the CPU
注意:在此示例中,如果兩個開關都打開,則僅播放第一首歌曲。 Pygame一次只能播放一首歌曲。 這使得撥動開關在這里似乎是一個錯誤的選擇。 目前尚不清楚兩個開關都打開時會發生什么情況。 考慮使用瞬時開關或旋轉開關 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.