I am trying to implement a piece of code to record voice from a microphone and control this from a bluetooth device by pressing two different buttons for star and stop. i am able to detect button (KEY_PAGEDOWN) press to start the recording, but when i press the other button to stop the recording and save to a .wav file, it does not work because the inner While loop keeps running forever and it does not get interrupted when i press the stop button (KEY_PAGEUP).
Any help is much appreciated
for event in bluetooth_device.read_loop(): if event.type == ecodes.EV_KEY and event.value == 1: keyevent = categorize(event) if keyevent.keycode == 'KEY_PAGEDOWN': recorder.start() print("Recording... press > to stop") while True: frame = recorder.read() audio.extend(frame) if keyevent.keycode == 'KEY_PAGEUP': recorder.stop() # save the audio to a wave file with wave.open("audio.wav", 'w') as f: f.setparams((1, 2, 16000, 0, "NONE", "NONE")) f.writeframes(struct.pack("h" * len(audio), *audio)) And to shed more light on what i am trying to do, the below code works fine on a try except block, but in my case i want to control the recording using a bluetooth device buttons
try: recorder.start() print("Recording... press Ctrl+C to stop") while True: frame = recorder.read() audio.extend(frame) except KeyboardInterrupt: print("Recording stopped") recorder.stop() # save the audio to a wave file with wave.open("audio.wav", 'w') as f: f.setparams((1, 2, 16000, 0, "NONE", "NONE")) f.writeframes(struct.pack("h" * len(audio), *audio)) print("Audio saved to audio.wav") finally: recorder.delete()