-1

Here's the code:

from playsound import playsound
import keyboard
key_to_sound = {
    '1': 'a.wav',
    '2': 'b.wav',
    '3': 'c.wav',
    '4': 'd.wav',
    '5': 'e.wav',
    '6': 'f.wav',
    '7': 'g.wav'
}
while True:
    key = keyboard.read_key()
    if key == 'num_1':
        break # breaks if num1 pressed
    elif key in key_to_sound:
        playsound(key_to_sound[key])

"@timgeb" helped me make this code, by the way. Is there any way I could make this module play more than one sound at once or do I have to use more complex modules?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Does this answer your question? [How do play audio (playsound) in background of Python script?](https://stackoverflow.com/questions/44472162/how-do-play-audio-playsound-in-background-of-python-script) – AMC Jan 18 '20 at 16:44
  • 1
    Do not add answers to questions. If you want to post an answer to your own question, fine post a separate answer. I think however it already has an answer that is exactly what you have, so there is really no need. Apart from that your explanation for the answer makes little sense. I don't see the difference. – Clifford Jan 18 '20 at 16:56
  • Who is "timgeb"? What does it refer to? Some other Stack Overflow question? [This Stack Overflow question](https://stackoverflow.com/questions/59798967/how-could-i-make-my-program-play-sounds-according-to-a-key-is-pressed-statement)? – Peter Mortensen Dec 07 '21 at 13:01

1 Answers1

0

From the documentation:

There’s an optional second argument, block, which is set to True by default. Setting it to False makes the function run asynchronously.

Thant means it will return immediately while the sound continues to play, so to play two sounds:

playsound( sound1, False ) ;
playsound( sound2, False ) ;

The second playsound could equally be blocking:

playsound( sound1, False ) ;
playsound( sound2, True ) ;
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • @Supremayro420 Did you follow the link, or read their answer? It's right there. – AMC Jan 18 '20 at 16:43
  • @Supremayro420 : I think I just explained that. – Clifford Jan 18 '20 at 16:45
  • @Supremayro420 Did it work? I am not a Python programmer, and have never tried it. I just read the documentation! – Clifford Jan 18 '20 at 16:47
  • @Clifford yeah just tried adding a false statement at the latest line which runs all the playsound thing, i would normally have to do playsound('oof.wav', False) – random person Jan 18 '20 at 16:53