Can I play sound through a speaker other than the standard with python on windows?

I have problems waking up in the morning, so I want to make an alarm clock with a python that will make me get up.

I am currently using my computer with two headphones connected to the audio jack on my monitor, which is connected to my computer via HDMI.

I would like to play audio files every day through speakers for laptops, and not through headphones. And I don’t want every night to sort through the headphones.

Can this be done?

I'm not looking for the ability to switch speakers by default. I just want to play the sound through the speaker, which is not the default speaker, which I know, because this happens from time to time with programs.

+6
source share
2 answers

I'm not sure if you can choose which audio device to output using winsound, but PyAudio is cross-platform and can output sound depending on which device you need. PyAudio is available at:

pip install --allow-external pyaudio --allow-notverified pyaudio pyaudio

In particular, the pyAudio.PyAudio () function. open () accepts an argument using "input_device_index", the pointer of the input device to use. Unspecified (or None) uses the device by default. Ignored if the input is False.

Docs and example: https://people.csail.mit.edu/hubert/pyaudio/docs/

Finding Which Device: List All Audio Devices Using pyaudio (Portal Binding) Python

+5
source

As an alternative to PyAudio, you can try python-sounddevice , which also uses PortAudio (like PyAudio), but it's probably easier to install and use.

0
source

All Articles