SongRec icon indicating copy to clipboard operation
SongRec copied to clipboard

Songrec registers itself as an MPRIS client and blocks media keys

Open frainz-de opened this issue 4 years ago • 5 comments

When I open SongRec, it registers itself as an MPRIS client and sometimes blocks the media keys. To reproduce:

  • open SongRec
  • type dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep org.mpris
  • The list contains string "org.mpris.MediaPlayer2.SongRec

See this article in the Arch Wiki for more information about MPRIS

frainz-de avatar Jan 10 '22 15:01 frainz-de

Hello,

Thanks for reporting. @Hummer12007, do you have any clue about the concerned issue?

Regards,

marin-m avatar Jan 22 '22 13:01 marin-m

@frainz-de, Sometimes I leave a muted video open on MPV in the right-up corner of my screen when doing stuff. That can be an issue if, for example, I start listening to Spotify. In this case, I end up in a situation where I can't control Spotify since my media keys are stuck with MPV. My solution was to use playerctl wrapped up in a shell script.

...
...
case $1 in
    'play_pause')
        if [ "$(/usr/bin/pgrep -x spotify)" ];
        then
            /usr/bin/playerctl --no-messages --player=spotify play-pause &
            exit
        elif [ "$(/usr/bin/pgrep -x mpv)" ];then
            if [[ "$mpv_players" == *"$active_window_pid"* ]]; then
                /usr/bin/playerctl --no-messages --player="mpv.instance$active_window_pid" play-pause &
                exit
            else
                /usr/bin/playerctl --no-messages --player=mpv play-pause &
                exit
            fi
        else
            /usr/bin/playerctl --no-messages play-pause &
            exit
        fi
    ;;
...
...

Basically, if Spotify is open, use playerctl to control Spotify above anything else that might be registered as an MPRIS player. I mean.. Who opens Spotify if not to listen to stuff, right? That way, I'm not stuck with that MPV video anymore. Not quite relevant here, but, for understanding purposes of the script, if another MPV is open alongside the in the right-top corner, control the one that's in focus.

rizzini avatar Oct 30 '23 15:10 rizzini

I also made a shell script to work around songrec's seemingly high priority capture of media keys vs other media players. It doesn't need playerctl and works using dbus

#!/bin/bash

# send media keys like play/pause/prev/next to player of choice in order of choice.
# helps combat media players who aggressively capture media keys
# assign keyboard shortcut in your desktop environment to call this script like:
# for media_play_pause or CTRL+ALT+SHIFT+SPACE, run script.sh play_pause
# for media_next or CTRL+ALT+>, run script.sh next
# for media_prev or CTRL+ALT+<, run script.sh prev

# Define player priority
# only the first player detected as running will be sent the media key commands
player_order=("youtube-music-desktop-app" "spotube" "spotify" "clementine" "vlc" "chromium" "firefox")

debug="no"



# Function to check if a player is running using MPRIS
is_player_running() {
  player_interface="$1"
  mpris_interface="/org/mpris/MediaPlayer2"

  # check if the player interface is running
  qdbus "$player_interface" "$mpris_interface" > /dev/null 2>&1
  if [[ $? -eq 0 ]]; then
    if [ $debug = "yes" ]; then echo "Player detected on: $player_interface"; fi
    return 0
  else
    if [ $debug = "yes" ]; then echo "Player not detected"; fi
    if [ $debug = "yes" ]; then echo; fi
    return 1
  fi

}

# Function to send media key command to the running media player
send_media_key() {
  player_interface=$1
  player_command=$2
  if [ $debug = "yes" ]; then echo; fi

  # Combine echo statements for different player types
  if [ $debug = "yes" ]; then echo "Executing: qdbus $player_interface $mpris_interface $player_command"; fi

  # Execute the media key command
  qdbus "$player_interface" "$mpris_interface" "$player_command"
}

# Define media key commands for each player
declare -A player_media_key_commands
player_media_key_commands=(
  ["next"]="org.mpris.MediaPlayer2.Player.Next"
  ["prev"]="org.mpris.MediaPlayer2.Player.Previous"
  ["play_pause"]="org.mpris.MediaPlayer2.Player.PlayPause"
)

# Command argument
command_arg=$1

if [ $debug = "yes" ]; then echo "Command argument: $command_arg"; fi
if [ $debug = "yes" ]; then echo; fi

# Loop through players in the specified order
for player in "${player_order[@]}"; do
  if [ $debug = "yes" ]; then echo "Checking player: $player"; fi
  player_interface=$(qdbus org.mpris.MediaPlayer2.* | grep "$player")

  if is_player_running "$player_interface"; then

    case $command_arg in
      "next" | "prev" | "play_pause")
        send_media_key "$player_interface" "${player_media_key_commands[$command_arg]}"
        ;;
      *)
        echo "Invalid option."
        echo "Usage: $0 {next|prev|play_pause}"
        exit 1
        ;;
    esac
    exit 0
  fi
done

if [ $debug = "yes" ]; then echo "No running media player found."; fi
exit 1

azizLIGHT avatar Dec 23 '23 07:12 azizLIGHT

Removing mpris by default does solve the media key blocking issue. However mpris is still useful to show metadata from internet radio streams and livestreams. Would it be possible to make as an option inside a config/setting or cli switch? I'm not sure how you could build the latest flatpak with the mpris option?

If you want mpris and can deal with blocked media keys with workarounds, you can either downgrade the flatpak with sudo flatpak update --commit=654c8d11c44d0543403b8d605221b9cfa691b213e7dbed7822050b1a105854b0 com.github.marinm.songrec or build with mpris option for newest updates

azizLIGHT avatar Dec 24 '23 19:12 azizLIGHT