I was using code from this answer to turn volume up and down for certain program (BS.player in this case), but I'm having problems when calling get_master_volume function. I'm using it inside pyHook's "OnKeyboardEvent", here's that part of code:
def get_master_volume():
config.read('%s/config.cfg'%os.path.abspath(os.path.dirname(__file__)))
proc = subprocess.Popen('%s sget Master '%config.get('main', input1), shell=True, stdout=subprocess.PIPE) #config.get gives FULL and correct path to bsplayer.exe
amixer_stdout = proc.communicate()[0].split('\n')[4]
proc.wait()
find_start = amixer_stdout.find('[') + 1
find_end = amixer_stdout.find('%]', find_start)
return float(amixer_stdout[find_start:find_end])
def set_master_volume(volume):
config.read('%s/config.cfg'%os.path.abspath(os.path.dirname(__file__)))
val = float(int(volume))
proc = subprocess.Popen('%s sset Master '%config.get('main', input1) + str(val) + '%', shell=True, stdout=subprocess.PIPE) #config.get gives FULL and correct path to bsplayer.exe
proc.wait()
def OnKeyboardEvent(event):
#nothing important
if config.has_option('main', input1):
set_master_volume(get_master_volume() - 1)
print "New volume:", get_master_volume()
This is error:
File "c:/Users/Amar/Documents/volume+/main.py", line 53, in get_master_volume
amixer_stdout = proc.communicate()[0].split('\n')[4]
IndexError: list index out of range
Can anyone explain me why this error fires and what's causing it (and what should I do to fix it)?
Best regards!
EDIT:
When I print proc.communicate() out I get this errors, what this means?
proc.communicate:'C:\Program' is not recognized as an internal or external comma
nd,
operable program or batch file.
('', None)
EDIT 2:
New error after I fixed bug in my config which was pointing on wrong bsplayer.exe path:
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in Keybo
ardSwitch
return func(event)
File "c:/Users/Amar/Documents/volume+/main.py", line 101, in OnKeyboardEvent
set_master_volume(get_master_volume() - 1)
File "c:/Users/Amar/Documents/volume+/main.py", line 54, in get_master_volume
amixer_stdout = proc.communicate()[0].split('\n')[4]
File "c:\Python27\lib\subprocess.py", line 798, in communicate
stdout = _eintr_retry_call(self.stdout.read)
File "c:\Python27\lib\subprocess.py", line 478, in _eintr_retry_call
return func(*args)
ValueError: I/O operation on closed file