Hardware Reference
In-Depth Information
random.shuffle(files)
player = vlc.MediaPlayer()
medialist = vlc.MediaList(files)
mlplayer = vlc.MediaListPlayer()
mlplayer.set_media_player(player)
mlplayer.set_media_list(medialist)
3. Next, add the code to set up the GPIO to use the physical buttons you added to
your breadboard and connected to your Raspberry Pi:
GPIO.setmode(GPIO.BCM)
PLAY_BUTTON=11
STOP_BUTTON=7
BACK_BUTTON=4
FORWARD_BUTTON=10
GPIO.setup(PLAY_BUTTON, GPIO.IN)
GPIO.setup(STOP_BUTTON, GPIO.IN)
GPIO.setup(BACK_BUTTON, GPIO.IN)
GPIO.setup(FORWARD_BUTTON, GPIO.IN)
In this code, you assign each GPIO that you are using to appropriately named
variables. Putting these in all uppercase is a convention often used in Python
when you don't intend to reassign or change those variables. he advantage of
this method is that you can refer to a pin (for example, PLAY_BUTTON) multi-
ple times in your code. his has two advantages: it's easy to see what the name
refers to; and if you change the GPIO pin for some reason the name only needs
to be updated in one place.
4. Next, amend the while loop code that reads and reacts to the input. he new
code uses methods from RPi.GPIO to detect when a physical button is pressed.
he changes are highlighted in bold in the following code (be sure to add the
hash marks, also in bold, to comment out the lines as shown):
while True:
# button = input(“Hit a button “)
if GPIO.input(PLAY_BUTTON):
print(“Pressed play button”)
if mlplayer.is_playing():
mlplayer.pause()
else:
mlplayer.play()
elif GPIO.input(STOP_BUTTON):
print(“Pressed stop button”)
mlplayer.stop()
random.shuffle(files)
Search WWH ::




Custom Search