Commit fb8eb3eb authored by santiago duque's avatar santiago duque

sounds now working when keyboard keys pressed, i can read a whole local directory

parent 5e7ee5ed
No preview for this file type
import os
from os.path import isfile, join
import pygame
import time
import signal
import keyboard
dir_path = os.getcwd()
sound_dir = os.getcwd() + "/sounds"
contentlist = os.listdir(sound_dir)
sound_array = []
cut_current_sound = False
channel = False
current_sound_index = -1
def setup_sounds():
for content in contentlist:
if isfile(join(sound_dir, content)):
if content.endswith(".mp3") or content.endswith(".wav"):
sound = pygame.mixer.Sound(join(sound_dir, content))
sound_array.append(sound)
print(sound_array)
def play_sound(index):
global cut_current_sound
global current_sound_index
global channel
if not cut_current_sound:
current_sound_index = index
channel = sound_array[current_sound_index].play()
while channel.get_busy():
pygame.time.wait(100) # ms
print("Playing...")
print("Finished.")
""" todo
make sounds stop when new key pressed
else:
if current_sound_index == index:
current_sound_index = index
channel = sound_array[current_sound_index].play()
while channel.get_busy():
pygame.time.wait(100) # ms
print("Playing...")
print("Finished.")
else:
print("STOP.")
"""
pygame.init()
setup_sounds()
while True:
try: # used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
play_sound(0)
elif keyboard.is_pressed("w"):
print("You Pressed A Key!")
play_sound(1)
elif keyboard.is_pressed("e"):
print("You Pressed A Key!")
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
sound1 = pygame.mixer.Sound('/Users/santiagoduque/Documents/WEBPROJECTS/apps/python-audio-player/test.wav')
sound1.play()
#time.sleep(5.5)
signal.pause()
# time.sleep(5.5)
# signal.pause()
File added
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