Search code examples
pythonlinuxraspberry-pivolumewebradio

Control the volume of Volumio


I have set up a Raspberry Pi with the Volumio player. Now I want to control the volume with a rotary encoder. And also want to pause or play the current song.

#!/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)

This is the coder I already have. But when I start it, and try to set the volume +1, I just get an error.

This one:

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

It would be great if someone can help me and tell me how to do the pause/play :)


Solution

  • I don't think there is something wrong in your code but the error say that mpc executable is not in your path (No such file or directory).

    Try to replace mpc by the absolute path or make it accessible from your python script.