1

I have a small script I want to have running in the background that would pause Spotify music if any other program starts playing audio - e.g. video, browser, game..

I got the spotify part working even without their API using win32api and SendMessage, however I can't seem to find any way of cheching whether other programs are playing audio.

I tried looking at windows mixer, maybe even at the jack connector, nothing..

Here's the code for it, but it's missing the check function and a loop

Bonus point, if you seen a program that does this thing, I'm all for it, it was even suggested to Spotify but they never responded.


def f_click(pycwnd):
        x=800
        y=840
        lParam = y << 16 | x
        pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);
        pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);

def get_whndl():
        whndl = win32gui.FindWindowEx(0, 0, None, 'Spotify Premium')
        return whndl

def make_pycwnd(hwnd):       
        PyCWnd = win32ui.CreateWindowFromHandle(hwnd)
        return PyCWnd
        
whndl = get_whndl()
pycwnd = make_pycwnd(whndl)
f_click(pycwnd)
Poody
  • 325
  • 3
  • 13
  • I've seen this asked quite a few times, so for kicks here is a list of every SO question I can find in `<` 10 Minutes. This question encapsulates Audio and possibly system processes, both of which are dealt with by native APIs. That means there would need to be an existing python library which wraps all these potential OS API calls. This is what you get with PyAudio, but I don't believe there is anything that fits your needs in existence – fdcpp Jan 08 '21 at 09:52
  • [How to listen to audio output by application](https://stackoverflow.com/questions/64128779/how-to-listen-to-audio-output-by-application?r=SearchResults&s=4|21.3579), [How to check programmatically with python if any other application is playing audio/video](https://stackoverflow.com/questions/64645989/how-to-check-programmatically-with-python-if-any-other-application-is-playing-au), [How to listen to audio output by application](https://stackoverflow.com/questions/64128779/how-to-listen-to-audio-output-by-application) – fdcpp Jan 08 '21 at 09:53
  • [How can I get the title of the currently playing media in windows 10 with python](https://stackoverflow.com/questions/65011660/how-can-i-get-the-title-of-the-currently-playing-media-in-windows-10-with-python) [Check if audio playing with Python on Windows 10](https://stackoverflow.com/questions/59636713/check-if-audio-playing-with-python-on-windows-10) and not python, [Check what programs are playing audio?](https://stackoverflow.com/questions/14850951/check-what-programs-are-playing-audio) – fdcpp Jan 08 '21 at 09:53
  • Outside of that, it will require creating something loop back system audio a la [Jack Audio](https://jackaudio.org/faq/jack_on_windows.html), but monitoring that will just tell you that there is audio, not where it is coming from. – fdcpp Jan 08 '21 at 09:53
  • I don't know if `DirectSound`, `ASIO` or `WASAPI` have a means of checking which processes are interacting with them, in which case you would need to check system processes potentially. You are probably going to need to step away from python, or at the least write your own `C`/`C++` library which you can then interface with using python. – fdcpp Jan 08 '21 at 09:57

1 Answers1

0

You can use Mic which could help with voice commands from your mouth as well.

import numpy as np
import sounddevice as sd


def is_sound_playing_windows():
    duration = 1000  # in seconds
    recording = sd.rec(int(duration), channels=2, blocking=True)
    volume_norm = np.linalg.norm(recording) * 10
    res = int(volume_norm)
    return res >= 3  # tweak it depending on your typing noise 

For Windows, I haven't found a way to actually a way to programmatically check if any software is producing sound. It's hard also because I have open websites that offer the functionality to mute as well, so Windows might not know they are actually muted.

Zack Light
  • 167
  • 2
  • 11