【问题标题】:Suspend script doesn't pause media players暂停脚本不会暂停媒体播放器
【发布时间】:2015-02-12 23:34:48
【问题描述】:

我为 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 有关。

【问题讨论】:

  • 即使您已经处理了totem,您是否要执行dbus-send ...?如果没有,请将dbus-send ... 放入else 块中,并放在初始if ... 块中。祝你好运。
  • 谢谢,谢特。我会试试的。
  • *在尝试了这个建议后* 看起来那行不通。虽然我能够继续编写脚本,但当我挂起到磁盘时它不会停止媒体播放器。哎呀,我认为在当前情况下(见here)系统无法从暂停状态返回,所以我必须进行硬重启。另请注意,我使用的是用于 Elementary OS 的 Freya 测试版。
  • 因为dbus-sendtotem 似乎是执行“繁重”的两个程序,我会使用man dbus-send 等,看看是否有调试/详细模式(通常-v, -vv, -vvv) 所以程序会告诉你它已经走了多远。 (我对您的工作没有经验,我只是提供一般性观察)。祝你好运!

标签: linux bash media-player sleep dbus


【解决方案1】:

我终于可以做到了。但我不得不广泛使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多