暂停脚本不会暂停媒体播放器

时间:2014-12-14 21:40:36

标签: linux bash media-player sleep dbus

我为Elementary OS编写了一个脚本,用于暂停所有正在运行的媒体播放器。这是因为在暂停之前声音可能非常响亮。可悲的是,我无法让它正常工作。

#! /bin/sh
# 
# Copyright (c) Jason Anderson 2014 <sirius-C at iname dot com>
# License: GPL-2
# * December 1, 2014
# - Version 0.1
# - Initial version
# * December 4, 2014
# - Version 0.2
# - Changed it for Totem video player
# - This is compatible with Audacious, Clementine, and VLC
# - Doesn't work for SMplayer
# * December 13, 2014
# - Cleaned up the code a little (added "suspend" to the case arguments).
# - Found out Banshee works, too.
# * December 14, 2014
# - Version 0.3
# - Refactored D-Bus code that sends the pause signal.

# Function for pausing the audio of each MPRIS-enabled media player.
pause_music() {
# Walk through the available media players via a call to qdbus and tell each of them
# to pause.  Usually the user has just one media player going, but we don't know which
# one.  So we have to do it this way.
  for i in $(qdbus org.mpris.MediaPlayer2.*)
    do
# Had a problem with Totem video player.  This works for the currently running
# instance of Totem.
      if [[ $i =~ "totem" ]]; then
# Don't know how this works for multiple instances of Totem.  But then again, who
# has multiple versions of Totem running at once?
         logger "[pause_media_players] Pausing Totem..."
         totem --pause.
      fi

      logger "[pause_media_players] Pausing the media player..."

      dbus-send --print-reply --dest=$i /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
      logger "[pause_media_players] ...Okay, they've been paused."
  done
}

logger "[13_pause-media-players] Started the pausing script."

# TODO: use another D-Bus application that's installed by default (i.e. gdbus).

# We need to use dbus-send so as to pause all of the media players that are listening via MPRIS.
case "$1" in
     hibernate|suspend)
       logger "[13_pause-media-players] We're hibernating.  Going down!"
       pause_music
            ;;
     resume|thaw)
       logger "[13_pause-media-players] We're unthawing.  Rise and shine!"
       pause_music
            ;;
# If we have anything else than a hibernate or a thaw, try to exit gracefully.
     *) 
       exit $NA 
            ;;
esac

exit 0

我认为可能是当计算机从暂停状态恢复时,它会恢复正在运行的媒体播放器。结果证明这也是错误的。我还以为这是脚本执行的顺序。这也不是问题。

所以我无法弄清楚它是否只是脚本的语法,或者它与D-Bus有关。

1 个答案:

答案 0 :(得分:0)

我终于能够做到了。但我不得不广泛使用setuid,我认为这不是很安全。无论如何,这是完成的脚本:

#!/bin/bash

# Function for pausing the audio of each MPRIS-enabled media player.
pause_music() {
    # This has to get the current address for the X server's DISPLAY variable.
    # ...How I get that, I do not know. 
    DISPLAY=":0"
    export DISPLAY
    object_path="/org/mpris/MediaPlayer2"
    method="org.mpris.MediaPlayer2.Player.Pause"
    # Use the users command and go through each user that's logged in.
    for user in $(users); do
      user_id=$(id -u $user)
      # Here's the hard part: making the system think that the originating user issued the command.
      # Usually when you call mdbus2 it returns a list of services on the session bus, which is what
      # we need.
      output=$(setuid $user_id mdbus2 | grep MediaPlayer2)
      # Now let's check to see if we really did have some output from that mdbus2 query.
      if [ -z $output ]; then
        logger "[$0] No media player detected."
        exit 0
      # Use pgrep to see if an instance of Totem is running.
      else if [ $(pgrep totem) ]; then
      # Apparently, this is the only way I know how to pause a Totem session already
      # in progress.
        logger "[$0] Now attempting to pause the media player..."
        totem --pause
        logger "[$0] ...Okay, the media player been paused."
      else
        logger "[$0] Now attempting to pause the media player..."
        setuid $user_id gdbus call --session --dest $output --object-path $object_path --method $method
        logger "[$0] ...Okay, the media player been paused."
      fi
    done
}

case "$1" in
     hibernate|suspend)
       logger "[$0] We're hibernating.  Going down!"
       pause_music
            ;;
     resume|thaw)
       logger "[$0] We're thawing.  Rise and shine!"
            ;;
# If we have anything else than a hibernate or a thaw, try to exit gracefully.
     *) 
       exit 0
            ;;
esac

exit 0