如何使用python获取Windows 10中当前正在播放的媒体的标题 [英] How can I get the title of the currently playing media in windows 10 with python
问题描述
在Windows 10中播放音频时,无论是来自Spotify,Firefox还是游戏.当您调大音量时,窗户的角落会出现一个东西,上面写着歌曲艺术家,标题和正在播放的应用程序(如下面的照片所示)(有时,如果游戏正在播放声音,它只会说出正在播放声音的应用程序)>
我想以某种方式使用python获取该数据.我的最终目标是,如果某个应用正在播放我不喜欢的内容(例如广告),则将其静音.
我正在获取窗口的标题以获取歌曲信息.通常,应用程序名称会显示在标题中,但是在播放歌曲时会显示歌曲名称.这是一个返回所有窗口标题的列表的函数.
来自__future__的 导入print_function导入ctypesdef get_titles():EnumWindows = ctypes.windll.user32.EnumWindowsEnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool,ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int))GetWindowText = ctypes.windll.user32.GetWindowTextWGetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthWIsWindowVisible = ctypes.windll.user32.IsWindowVisible标题= []def foreach_window(hwnd,lParam):如果IsWindowVisible(hwnd):长度= GetWindowTextLength(hwnd)buff = ctypes.create_unicode_buffer(长度+ 1)GetWindowText(hwnd,buff,长度+1)titles.append(buff.value)返回TrueEnumWindows(EnumWindowsProc(foreach_window),0)返回标题
Whenever audio is playing in windows 10, whether it is from Spotify, Firefox, or a game. When you turn the volume, windows has a thing in the corner that says the song artist, title, and what app is playing like the photo below (sometimes it only says what app is playing sound if a game is playing the sound)
I want to somehow get that data with python. My end goal, is to mute an application if it is playing something I don't like, such as an advertisement.
I am getting the titles of the windows to get the song information. Usually, the application name is displayed in the title, but when it is playing a song, the song name is shown. Here is a function that returns a list of all the window titles.
from __future__ import print_function
import ctypes
def get_titles():
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
titles = []
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)
return titles
这篇关于如何使用python获取Windows 10中当前正在播放的媒体的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!