MPRIS toggler

This commit is contained in:
osmarks 2023-08-20 20:20:32 +01:00
parent 69be3301d8
commit df51de06e6
2 changed files with 27 additions and 1 deletions

View File

@ -50,4 +50,5 @@ This comes with absolutely no guarantee of support or correct function, although
* `yearbox.html` - DokuWiki-type yearbox prototype for Minoteaur (I think this actually contains an off-by-one error somewhere; it isn't what's actually in use).
* `arbitrary-politics-graphs` - all you need to run your own election campaign.
* `heavbiome` - some work on biome generation with Perlin noise.
* `block_scope.py` - Python uses function scoping rather than block scoping. Some dislike this. I made a decorator to switch to block scoping.
* `block_scope.py` - Python uses function scoping rather than block scoping. Some dislike this. I made a decorator to switch to block scoping.
* `mpris_smart_toggle.py` - playerctl play-pause sometimes does not play or pause the media I want played or paused (it seems to use some arbitrary selection order). This does it somewhat better by tracking the last thing which was playing.

25
mpris_smart_toggle.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
from mpris2 import get_players_uri, Player
from os.path import expanduser
savepath = expanduser("~/.local/share/last_media.txt")
players = list(get_players_uri())
last = None
try:
with open(savepath) as f:
last = f.read()
except:
pass
players = list(get_players_uri())
for player_uri in players:
player = Player(dbus_interface_info={"dbus_uri": player_uri})
if player.PlaybackStatus == "Playing":
player.Pause()
with open(savepath, "w") as f:
f.write(player_uri)
break
else:
if last in players:
Player(dbus_interface_info={"dbus_uri": last}).Play()
else:
player.Play()