Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Simple python script that mutes sound when Spotify app runs an ad

Hey guys, was a bit distracted by the fact that Spotify Free is killing the mood sometimes in a foreign language, so decided to create a script that mutes all the sound whenever there is an ad playing.

This script only works on Windows.

This script get windll libraries and uses them to create a process name list (mostly copied code).

After the list is built, it is checked for Process names "Advertisement" and "Spotify" to see if an ad is playing. These names are specific to the moment when ad is being played in Spotify.

The script is run in an interval, and does not fetch data real-time, so has small delays in runtime. As it is short and easily processed, does not load up CPU and doesn't leak memory.

The code: (Requires ctypes and pycaw libraries)


import ctypes #process find
import time #sleep
from pycaw.pycaw import AudioUtilities #mute


while True:
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))


/r/Python
https://redd.it/cefijn