0

I found an application that allows you to do this, and I wondered if this could be done with any built-in programs (I don't mean the Caps Lock, Num Lock radio buttons, their sound can be turned on in the control panel)? This can be done on python with the code below, but my winsound.PlaySound doesn’t want to find the file and just plays the system sound of the error. This answer did not help (either through sys or creating a folder).

The code:

import winsound import keyboard duration = 250 q = 300 while True: try: if keyboard.is_pressed('q'): winsound.PlaySound('C:\\some.wav',winsound.SND_FILENAME) winsound.Beep(q, duration)#Since PlaySound does not want to search, you have to do it through squeak except: break 
2
  • What do you mean by "doesn't want to find the file"? You always need to tell the program where to find the file, there's no "finding"... Commented Oct 7, 2019 at 13:16
  • @h4z3, Yes, but even if I specify the path, then mp3 or wav files (different tried) still don't play Commented Oct 7, 2019 at 13:19

1 Answer 1

0

I discovered something similar some months ago in VB.net
Thus I suggest you to not use the winsound but the simpleaudio package.

import simpleaudio as sa filename = 'myfile.wav' wave_obj = sa.WaveObject.from_wave_file(filename) play_obj = wave_obj.play() play_obj.wait_done() # Wait until sound has finished playing 




from pynput.keyboard import Listener import simpleaudio as sa filename = 'music.wav' wave_obj = sa.WaveObject.from_wave_file(filename) def on_press(key): if key.char == 'q': play_obj = wave_obj.play() play_obj.wait_done() with Listener(on_press=on_press) as listener: listener.join() 
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe I'm doing something wrong? I did everything as you said, but after pressing q, the sound does not play and the program ends. while True: try: if keyboard.is_pressed('q'): filename = 'some.wav' wave_obj = sa.WaveObject.from_wave_file(filename) play_obj = wave_obj.play() play_obj.wait_done() elif keyboard.is_pressed('w'): winsound.Beep(w, duration) else: pass except: break
added an implementation with key listener

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.