控制Volumio的音量

时间:2015-05-05 10:48:02

标签: python linux raspberry-pi volume webradio

我已经使用Volumio播放器设置了Raspberry Pi。 现在我想用旋转编码器控制音量。 并且还想暂停或播放当前的歌曲。

#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# This class uses a standard rotary encoder with push switch
#

import sys
import time
from rotary_class import RotaryEncoder
import subprocess

# Define GPIO inputs
PIN_A = 21  
PIN_B = 16  
BUTTON = 4

# This is the event callback routine to handle events
def switch_event(event):
    if event == RotaryEncoder.CLOCKWISE:
        print "Volume up"
        subprocess.call(['mpc', 'volume', '+1'])

    elif event == RotaryEncoder.ANTICLOCKWISE:
        print "Volume down"
        subprocess.call(['mpc', 'volume', '-1' ])

    elif event == RotaryEncoder.BUTTONDOWN:
        print "Pause/Play"      

#   elif event == RotaryEncoder.BUTTONUP:
#       print "Button up"
    return

# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)

while True:
    time.sleep(0.5)

这是我已经拥有的编码器。 但是当我启动它并尝试设置音量+1时,我只是得到一个错误。

这一个:

Traceback (most recent call last):
  File "/home/pi/radio/rotary_class.py", line 87, in switch_event
    self.callback(event)
  File "./test_rotary_class.py", line 35, in switch_event
    subprocess.call(['mpc', 'volume', '-1' ])
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

如果有人可以帮助我并告诉我如何暂停/玩游戏会很棒。)

2 个答案:

答案 0 :(得分:1)

我认为您的代码中存在问题,但错误表明mpc可执行文件不在您的路径中(No such file or directory)。

尝试用绝对路径替换mpc或者从python脚本中访问它。

答案 1 :(得分:0)

  

如果有人可以帮助我并告诉我如何暂停/玩游戏会很棒。)

mpc toggle

是您正在寻找的。