dietervanhoof / polybar-spotify-controls

MIT License
87 stars 8 forks source link

Playing and Pausing a youtube video toggles the play icon #14

Closed sidequestboy closed 4 years ago

sidequestboy commented 4 years ago

Why does this happen?

spotify

I am running pulseaudio

dietervanhoof commented 4 years ago

Huh. Seems like changing the YouTube playing vs not playing status also triggers the event handler. Perhaps we'd need to implement some kind of check on the source of the event.

The event is interpreted in this block.

How familiar are you with programming? Perhaps you could log some more info on the event itself and see if it's possible to see whether it comes from Spotify or not? Otherwise I may just try and spin up my Linux again to have a go at it.

nischalbasuti commented 4 years ago

I was able to fix it by modifying the event_handler method:

def event_handler(*args, **kwargs):
    """event handler for the receiver."""
    """arg[1] contains metadata"""
    """arg[1][1] contains PlaybackStatus"""
    data = unwrap(args)
    print(data)
    if 'spotify' in data[1]['Metadata']['mpris:trackid']: # Checking for spotify here
        if data[1]['PlaybackStatus'] == "Playing":
            print("Music is playing")
            system("polybar-msg hook playpause 2")
            """ Send IPC hook 2 to all bars for module playpause """
        if data[1]['PlaybackStatus'] == "Paused":
            print("Music is paused.")
            """ Send IPC hook 3 to all bars module playpause """
            system("polybar-msg hook playpause 3")
        """ Send IPC hook 2 to all bars for module spotify """
        system("polybar-msg hook spotify 2")

Here's an example for the contents of the data variable when using Spotify:

['org.mpris.MediaPlayer2.Player', {'Metadata': {'mpris:trackid': 'spotify:track:3BQHpFgAp4l80e1XslIjNI', 'mpris:length': 125666000, 'mpris:artUrl': 'https://open.spotify.com/image/ab67616d00001e02e3e3b64cea45265469d4cafa', 'xesam:album': 'Help! (Remastered)', 'xesam:albumArtist': ['The Beatles'], 'xesam:artist': ['The Beatles'], 'xesam:autoRating': dbus.Double(0.78, variant_level=1), 'xesam:discNumber': 1, 'xesam:title': 'Yesterday - Remastered 2009', 'xesam:trackNumber': 13, 'xesam:url': 'https://open.spotify.com/track/3BQHpFgAp4l80e1XslIjNI'}, 'PlaybackStatus': 'Paused'}, []]

And here's an example for youtube:

['org.mpris.MediaPlayer2.Player', {'PlaybackStatus': 'Paused'}, []]

Would you like me to open a PR @dietervanhoof ?

dietervanhoof commented 4 years ago

Perfect! Just as expected there are events being processed from other tools. I'd very much appreciate a PR! It's been a while since I last booted up my Linux machine and thus it's also been a while since I last worked on this.

Your contribution is much appreciated.

sidequestboy commented 4 years ago

thank you both! works great (: