1

I wrote a script which plays audio when you press any key (that's a letter), but I'm not getting any audio, the script itself actualy works but I don't hear anything

import os
import keyboard, string, random
import pygame as pg

letter = string.ascii_letters
digit = string.digits

pg.init()
pg.mixer.init()

ks1 = "C:\\keypress\\key-press-1.mp3"
ks2 = "C:\\keypress\\key-press-2.mp3"
ks3 = "C:\\keypress\\key-press-3.mp3"
ks4 = "C:\\keypress\\key-press-4.mp3"

keyList = [ks1, ks2, ks3, ks4]

def typing(keySound):
    if letter.find(keyboard.read_key())>-1:
        pg.mixer.music.load(keySound)
        pg.mixer.music.play()

def main():
    while True:
        keySound = random.choice(keyList)
        print(keySound)
        typing(keySound)

main()

Any help is appreciated!

Daburu
  • 109
  • 1
  • 11
  • 1
    This question comes up all the time. Essentially `music.play()` only *starts* the sounds playing. So your `main()` loop is rapidly starting a new sound before before the previous sound has time to complete. See this answer for usage of `get_busy()` - https://stackoverflow.com/a/35241284/1730895 – Kingsley Oct 23 '19 at 21:59
  • Also this question: https://stackoverflow.com/questions/7746263/how-can-i-play-an-mp3-with-pygame – Kingsley Oct 23 '19 at 22:05

0 Answers0