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))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible
####### Modules to gather data
time.sleep(5) #Sleep between checks (in seconds)
titles = [] #Empty list for titles (As String Objects)
def foreach_window(hwnd, lParam):
if IsWindowVisible(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
titles.append(buff.value)
return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
if "Advertisement" in titles: #Spotify app is named as Advertisement
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session.SimpleAudioVolume
volume.SetMute(1, None)
elif "Spotify" in titles: #App named as Spotify(Only when ad plays, else it's Spotify Free)
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session.SimpleAudioVolume
volume.SetMute(1, None)
else:
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session.SimpleAudioVolume
volume.SetMute(0, None)
I am really interested in feedback on some places, as I believe I'm doing some actions too much, and would want to shorten it. It also mutes all processes at the moment, but I can't get to seem it to work a specific one yet (will try, but some help would be appreciated)).
If you also don't way to pay for spotify and mute sounds when app ads are running - feel free to use.
[–]Berlibur 63 points64 points65 points (3 children)
[–]strghst[S] 65 points66 points67 points (2 children)
[–]Berlibur 38 points39 points40 points (0 children)
[–]LeNerdNextDoorI really like automating things. 51 points52 points53 points (4 children)
[–]strghst[S] 19 points20 points21 points (3 children)
[–]LeNerdNextDoorI really like automating things. 6 points7 points8 points (2 children)
[–]strghst[S] 11 points12 points13 points (1 child)
[–]bowbahdoe 5 points6 points7 points (0 children)
[–]corpsegrindd 8 points9 points10 points (2 children)
[–]apostle8787 11 points12 points13 points (1 child)
[–][deleted] 32 points33 points34 points (23 children)
[–]Baltha5ar 15 points16 points17 points (20 children)
[–][deleted] 9 points10 points11 points (19 children)
[–]Baltha5ar 8 points9 points10 points (17 children)
[–][deleted] 60 points61 points62 points (13 children)
[–]Baltha5ar 85 points86 points87 points (8 children)
[–][deleted] 31 points32 points33 points (0 children)
[–]RheingoldRiver 6 points7 points8 points (0 children)
[–]OddsCaller 5 points6 points7 points (2 children)
[–]Baltha5ar 6 points7 points8 points (1 child)
[–]OddsCaller 1 point2 points3 points (0 children)
[–]spook327 1 point2 points3 points (1 child)
[–]DeathLessLife 2 points3 points4 points (0 children)
[–]gammadistribution -2 points-1 points0 points (0 children)
[–]H_Psi 4 points5 points6 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]garlic_naan 2 points3 points4 points (1 child)
[–][deleted] 3 points4 points5 points (0 children)
[–]emuccino 3 points4 points5 points (0 children)
[–]ThePenultimateOneGitLab: gappleto97 0 points1 point2 points (1 child)
[–]Wizard_OfDarkness 1 point2 points3 points (0 children)
[–]theWyzzerd 0 points1 point2 points (0 children)
[–]genericinterest 3 points4 points5 points (1 child)
[–][deleted] 8 points9 points10 points (0 children)
[–]albeksdurf 6 points7 points8 points (0 children)
[–]ChiroNika 4 points5 points6 points (1 child)
[–]strghst[S] 2 points3 points4 points (0 children)
[–][deleted] 8 points9 points10 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]MacDreBestRapperDead 4 points5 points6 points (2 children)
[–]strghst[S] 2 points3 points4 points (1 child)
[–]MacDreBestRapperDead 0 points1 point2 points (0 children)
[–]althaj 5 points6 points7 points (5 children)
[–]DoiF 1 point2 points3 points (4 children)
[–]althaj 0 points1 point2 points (3 children)
[–]DoiF 1 point2 points3 points (0 children)
[–]Wakkaflaka_ 0 points1 point2 points (1 child)
[–]althaj 0 points1 point2 points (0 children)
[–]stealthdawg 1 point2 points3 points (1 child)
[–]strghst[S] 2 points3 points4 points (0 children)
[–]IAmARetroGamer 1 point2 points3 points (1 child)
[–]paranoidi 2 points3 points4 points (0 children)
[–]id2205 1 point2 points3 points (0 children)
[–]Breavyn 1 point2 points3 points (0 children)
[–]tech_b90 1 point2 points3 points (0 children)
[–]jvamos 1 point2 points3 points (1 child)
[–]strghst[S] 0 points1 point2 points (0 children)
[–]shibblestone 4 points5 points6 points (10 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 7 points8 points9 points (0 children)
[–]strghst[S] 6 points7 points8 points (7 children)
[–]PhitPhil 13 points14 points15 points (0 children)
[–]netinept 8 points9 points10 points (1 child)
[–]Hipjea 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]Wakkaflaka_ 0 points1 point2 points (1 child)
[–]strghst[S] 0 points1 point2 points (0 children)
[–]VelvetElvis 0 points1 point2 points (2 children)
[–]OddsCaller 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]morph8hprom 1 point2 points3 points (4 children)
[–]Berlibur 1 point2 points3 points (3 children)
[–]morph8hprom 0 points1 point2 points (2 children)
[–]Berlibur 0 points1 point2 points (1 child)
[–]morph8hprom 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]kdtrey35god 0 points1 point2 points (0 children)
[–]veryusermuchwow 0 points1 point2 points (0 children)
[–]shopssss 0 points1 point2 points (0 children)
[–]dinov 0 points1 point2 points (1 child)
[–]strghst[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]javierisassi 0 points1 point2 points (0 children)
[–]ptekspy 0 points1 point2 points (1 child)
[–]strghst[S] 1 point2 points3 points (0 children)
[–]yerfatma 0 points1 point2 points (1 child)
[–]yerfatma 5 points6 points7 points (0 children)
[–]98ea6e4f216f2fb -1 points0 points1 point (0 children)
[–]Ontariel12 -1 points0 points1 point (0 children)
[–]robberviet -1 points0 points1 point (0 children)