問題描述
無論何時(shí)在 Windows 10 中播放音頻,無論是來自 Spotify、Firefox 還是游戲.當(dāng)你打開音量時(shí),windows 的角落里會(huì)顯示歌曲藝術(shù)家、標(biāo)題以及正在播放的應(yīng)用程序,如下圖所示(有時(shí)它只顯示游戲正在播放聲音時(shí)哪個(gè)應(yīng)用程序正在播放聲音)
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)
我想以某種方式使用 python 獲取這些數(shù)據(jù).我的最終目標(biāo)是,如果應(yīng)用程序正在播放我不喜歡的內(nèi)容(例如廣告),則將其靜音.
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.
推薦答案
我正在獲取windows的標(biāo)題來獲取歌曲信息.通常,應(yīng)用程序名稱顯示在標(biāo)題中,但在播放歌曲時(shí),會(huì)顯示歌曲名稱.這是一個(gè)返回所有窗口標(biāo)題列表的函數(shù).
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
這篇關(guān)于如何使用 python 在 Windows 10 中獲取當(dāng)前正在播放的媒體的標(biāo)題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!