import asyncio from winrt.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager import win32com.client import pythoncom import time from resources import LineState, CLMgrMessage phone_mgr = "" lines = [] class PhoneLineEventHandler(): def OnDispOnLineMgrNotification(self, msg, param, returns=""): if msg == 0: print(CLMgrMessage.s[msg], LineState.s[param]) for line in lines: if line.DispState != LineState.Inactive: asyncio.run(try_pause()) return True asyncio.run(try_play()) return True # async def get_media_info(): # sessions = await MediaManager.request_async() # # This source_app_user_model_id check and if statement is optional # # Use it if you want to only get a certain player/program's media # # (e.g. only chrome.exe's media not any other program's). # # To get the ID, use a breakpoint() to run sessions.get_current_session() # # while the media you want to get is playing. # # Then set TARGET_ID to the string this call returns. # current_session = sessions.get_current_session() # if current_session: # there needs to be a media session running # #if current_session.source_app_user_model_id == TARGET_ID: # info = await current_session.try_get_media_properties_async() # # song_attr[0] != '_' ignores system attributes # info_dict = {song_attr: info.__getattribute__(song_attr) for song_attr in dir(info) if song_attr[0] != '_'} # # converts winrt vector to list # info_dict['genres'] = list(info_dict['genres']) # return info_dict # # It could be possible to select a program from a list of current # # available ones. I just haven't implemented this here for my use case. # # See references for more information. # raise Exception('TARGET_PROGRAM is not the current media session') async def try_play(): print("try_play") sessions = await MediaManager.request_async() current_session = sessions.get_current_session() if current_session: await current_session.try_play_async() async def try_pause(): print("try_pause") sessions = await MediaManager.request_async() current_session = sessions.get_current_session() if current_session: await current_session.try_pause_async() if __name__ == '__main__': phone_mgr = win32com.client.Dispatch("CLMgr.ClientLineMgr") phone_mgr_event = win32com.client.WithEvents("CLMgr.ClientLineMgr", PhoneLineEventHandler) for i in range(3): lines.append(phone_mgr.DispGetLine(i)) while True: #pythoncom.PumpWaitingMessages() time.sleep(0.1) # Don't use up all our CPU checking constantly