Commit 0e4dba85 authored by santiago duque's avatar santiago duque

now working with pygame keyboard

parent b0e4be43
import os import os
from os.path import isfile, join from os.path import isfile, join
import pygame import pygame
import time
import signal # from storage_media import USBDeviceHandler
import keyboard
from storage_media import USBDeviceHandler # import time
# import signal
# import keyboard
pygame.init()
# window = pygame.display.set_mode((300,300))
clock = pygame.time.Clock()
dir_path = os.getcwd() dir_path = os.getcwd()
sound_dir = os.getcwd() + "/sounds" sound_dir = os.getcwd() + "/sounds"
...@@ -14,7 +20,9 @@ cut_current_sound = False ...@@ -14,7 +20,9 @@ cut_current_sound = False
channel = False channel = False
current_sound_index = -1 current_sound_index = -1
usb_handler = USBDeviceHandler() contentlist = sorted(contentlist)
# usb_handler = USBDeviceHandler()
def setup_sounds(): def setup_sounds():
for content in contentlist: for content in contentlist:
...@@ -22,13 +30,14 @@ def setup_sounds(): ...@@ -22,13 +30,14 @@ def setup_sounds():
if content.endswith(".mp3") or content.endswith(".wav"): if content.endswith(".mp3") or content.endswith(".wav"):
sound = pygame.mixer.Sound(join(sound_dir, content)) sound = pygame.mixer.Sound(join(sound_dir, content))
sound_array.append(sound) sound_array.append(sound)
print(content)
print(sound_array) print(sound_array)
def play_sound(index): def play_sound(index):
global cut_current_sound global cut_current_sound
global current_sound_index global current_sound_index
global channel global channel
if not cut_current_sound: if not cut_current_sound:
current_sound_index = index current_sound_index = index
...@@ -36,48 +45,49 @@ def play_sound(index): ...@@ -36,48 +45,49 @@ def play_sound(index):
while channel.get_busy(): while channel.get_busy():
pygame.time.wait(100) # ms pygame.time.wait(100) # ms
print("Playing...") print("Playing...")
pygame.event.clear()
print("Finished.") print("Finished.")
""" todo """ todo
make sounds stop when new key pressed make sounds stop when new key pressed
else: else:
if current_sound_index == index: if current_sound_index == index:
current_sound_index = index current_sound_index = index
channel = sound_array[current_sound_index].play() channel = sound_array[current_sound_index].play()
while channel.get_busy(): while channel.get_busy():
pygame.time.wait(100) # ms pygame.time.wait(100) # ms
print("Playing...") print("Playing...pygame.init()")
print("Finished.") print("Finished.")
else: else:
print("STOP.") print("STOP.")
""" """
pygame.init()
setup_sounds() setup_sounds()
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
while True: if event.type == pygame.KEYDOWN:
try: # used try so that if user pressed other than the given key error will not be shown print("you pressed " + pygame.key.name(event.key))
if keyboard.is_pressed("q"): if event.key == pygame.K_q:
print("You Pressed A Key!") play_sound(0)
play_sound(0) elif event.key == pygame.K_w:
elif keyboard.is_pressed("w"): play_sound(1)
print("You Pressed A Key!") elif event.key == pygame.K_e:
play_sound(1) play_sound(2)
elif keyboard.is_pressed("e"): elif event.key == pygame.K_r:
print("You Pressed A Key!") play_sound(3)
play_sound(2)
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
play_sound(3)
except:
break # if user pressed a key other than the given key the loop will break
pygame.quit()
exit()
# time.sleep(5.5) # time.sleep(5.5)
# signal.pause() # signal.pause()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment