Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add Media widget #13

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

[FEAT] Add Media widget #13

wants to merge 5 commits into from

Conversation

da-rth
Copy link
Owner

@da-rth da-rth commented Feb 10, 2022

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.

image

@hexfactor
Copy link

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.

@n3rdly
Copy link

n3rdly commented Dec 15, 2023

I wish I had seen this FEAT before spending several hours last night trying to build a widget for this. 🤦 (Still had fun though 😅)

@Welpyes
Copy link

Welpyes commented Mar 27, 2024

@hexfactor hello i tried your media player workaround and it doesnt work and gives me these errors
image
is there smth im doing wrong?

@agoodlet
Copy link

agoodlet commented May 1, 2024

@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 != ""]

@ashwinjadhav818
Copy link

I have tried recreating this widget in Python v12, but failed every time, if anyone knows how to get it work, can you pls add a pr in My YASB fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants