yasb
yasb copied to clipboard
[FEAT] Add Media widget
Pull Request #XX
New widget, config validation, and stylesheet changes.
Description
Adds a win32 media player widget. Allows user to view / control currently playing media directly via the status bar.
The following code returns a json
object containing the information about currently playing media. It uses winsdk
rather than winrt
so works in Python 3.10.
# media.py
import winsdk.windows.media.control
import asyncio
import json
async def get_media_info():
session_manager = await winsdk.windows.media.control.GlobalSystemMediaTransportControlsSessionManager.request_async()
current_session = session_manager.get_current_session()
media_properties = await current_session.try_get_media_properties_async()
media_playback_info = {
"title": media_properties.title,
"artist": media_properties.artist,
"album": media_properties.album_title,
"album_artist": media_properties.album_artist,
"track": media_properties.track_number
}
print(json.dumps(media_playback_info))
asyncio.run(get_media_info())
When paired with a CustomWidget
, this can be used to display information about currently playing media in a widget.
# config.yaml
widgets:
media:
type: "yasb.custom.CustomWidget"
options:
label: "{data[title]} - {data[artist]}"
label_alt: "{data[album]}"
class_name: "media-widget"
exec_options:
run_cmd: 'python.exe PATH_TO_PYTHON_FILE' # Paste the above code into a Python file and reference it here
run_interval: 5000
return_format: "json"
Of course, this isn't a complete solution (no album artwork or media controls), but is a simple way to display basic media information in a widget, at least until the media_widget
branch is updated and merged.
I wish I had seen this FEAT before spending several hours last night trying to build a widget for this. 🤦 (Still had fun though 😅)
@hexfactor hello i tried your media player workaround and it doesnt work and gives me these errors
is there smth im doing wrong?
@Welpyes the problem for me was because there was a double space between python.exe
and the python file path. When it's splits on " " in the custom widget code it will end up with an array like ["python.exe", "", "path to media.py"]
but the python exe expects the first arg passed to it to the be the file path so in the subprocess it's essentially calling python.exe on nothing
if you delete one of the spaces in the run_cmd for this widget it should work or you can add something like the following to protect against this in general
self._exec_cmd = [item for item in self._exec_cmd if item != ""]