2

I'm working with Python 3.7 on Windows 10. I would like to detect if there is any audio playing on my computer or not. I was looking into win32api.GetVolumeinformation but I'm unable to get what I want.

When you control your audio you can see if there is a program playing and I want to achieve that.

audio controls without program

audio controls with program

metalbea
  • 43
  • 1
  • 7
  • Does this answer your question? [Python + winsound - check whether an audio file is being played](https://stackoverflow.com/questions/19976364/python-winsound-check-whether-an-audio-file-is-being-played) – APhillips Jan 07 '20 at 22:06
  • That dialog doesn't know, whether there is any audio playing currently. Open some media player, pause the playback, and hit the audio controls: No sounds is playing, but you're seeing your media player in the UI. So what are you *really* trying to accomplish here? – IInspectable Jan 07 '20 at 22:10
  • Unfortunately not, unless I've read it wrong that's about playing a file while I want to see if a program is playing audio. – metalbea Jan 07 '20 at 22:13
  • IInspectable, my main goal is to create a script that will pause the program that is playing when I'm leaving my computer. So I'll have to use face detection. But before I dive into that I wanted to be sure that I can play/pause the audio if there is any audio. I already can play/pause the audio programmatically – metalbea Jan 07 '20 at 22:17
  • 1
    I've found a solution using C# so I'm going to continue my project in C#. Is it okay to keep my question up in case someone else has the same problem or should I delete my question? – metalbea Jan 07 '20 at 23:34
  • 1
    @metalbea If the issue solved you can post an answer and accept it. Maybe it will help other people are searching on this topic. – Rita Han Jan 15 '20 at 08:47
  • @metalbea hey could you guide me with what to look for in C# to accomplish this ? Thank you – Alexandre Aug 12 '20 at 10:10
  • 1
    @Alexandre Here is [my code](https://github.com/metalbea/MediaAudioChecker/blob/master/MediaAudioChecker.cs) for checking if audio is playing and to press play/pause programmatically – metalbea Aug 17 '20 at 15:12
  • Thank you, I'm currently looking for the code to get the title off the image you posted above. Still helpfull thank you. – Alexandre Aug 20 '20 at 00:03
  • @Alexandre unfortunately I'm unable to help you with that. But I am curious to see your solution :D – metalbea Aug 22 '20 at 18:37
  • I only got this for now, you need to have chrome running with a tab opened to youtube. This is the only drawback that I've noticed : Unformatted code, receives a process name, find it, enum the windows and check the titles to contain Youtube. \\\\\\\\ procs = enum_processes(process_name=processName) for pid, name in procs: data = enum_process_windows(pid) if data: for item in data: if processName == "chrome.exe": if " - YouTube - Google Chrome" in item[1]:return item[1].replace(" - YouTube - Google Chrome","") – Alexandre Aug 27 '20 at 05:42
  • It's in Python 3 – Alexandre Aug 27 '20 at 05:44
  • Also, if you wanted to get the title off the playing media, check [this question](https://stackoverflow.com/questions/65011660/how-can-i-get-the-title-of-the-currently-playing-media-in-windows-10-with-python/66037406). – Wudfulstan Mar 27 '21 at 05:23

1 Answers1

2

Try this api using winrt: The enum options are listed here, but you can use mediaIs("PAUSED"), mediaIs("PLAYING") ect...

import asyncio, winrt.windows.media.control as wmc

async def getMediaSession():
    sessions = await wmc.GlobalSystemMediaTransportControlsSessionManager.request_async()
    session = sessions.get_current_session()
    return session

def mediaIs(state):
    session = asyncio.run(getMediaSession())
    if session == None:
        return False
    return int(wmc.GlobalSystemMediaTransportControlsSessionPlaybackStatus[state]) == session.get_playback_info().playback_status #get media state enum and compare to current main media session state

There are heaps more useful winrt APIs to control media on windows too here.

Wudfulstan
  • 129
  • 11