如何在Windows10通知中添加歌曲封面?win10toast/win11toast问题
Windows 10下如何在Spotify歌曲切换通知中显示封面图片?
我用Python结合Spotify API开发了一个程序,连接Spotify账号后,每当播放的歌曲切换时,会通过win10toast库发送包含歌曲名、歌手名的通知。但我想在通知旁添加当前播放歌曲的封面图片,却找不到实现方法。
我尝试改用win11toast库,它的文档完善,在Windows 11虚拟机中可正常显示图片,但在Windows 10系统中,通知仅预留了图片位置却无法显示图片——即便使用文档中的示例URL也无效,特此寻求帮助。
Win10toast 相关代码
获取Spotify播放数据
通过以下代码将歌曲名、歌手名、封面图片URL、歌曲URL分别赋值给name、artist、cover、url变量:
client_id = "CLIENT_ID" client_secret = "CLIENT_SECRET" redirect_uri = "REDIRECT_URL" auth_manager = spotipy.SpotifyOAuth( client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope="user-read-currently-playing" ) sp = spotipy.Spotify(auth_manager=auth_manager) showing = False results = sp.current_user_playing_track() name = results['item']['name'] artist = results['item']['artists'][0]['name'] cover = results['item']['album']['images'][0]['url'] url = results['item']['external_urls']['spotify']
发送通知实现
以下代码创建了点击通知打开歌曲链接的函数,指定本地Spotify图标文件,再通过win10toast库发送通知:
def open_url(): try: webbrowser.open_new(url) print('Opening URL...') except: print('Failed to open URL. Unsupported variable type.') file_name = 'logo.ico' toaster = ToastNotifier() toaster.show_toast( name, artist, icon_path=file_name, duration=5, callback_on_click=open_url )
Win11toast 相关代码
获取Spotify播放数据的代码与win10toast部分完全一致,以下是发送通知的代码——问题在于Windows 10系统下无法显示图标或图片,仅预留了空白位置:
toast(name, artist, icon=cover, on_click=url, audio={'silent': 'true'})
内容的提问来源于stack exchange,提问作者Andrearoma2
相关产品推荐
相关产品推荐

