forked from jwayneroth/fmu-pi-oled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpc.py
87 lines (75 loc) · 2.16 KB
/
mpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
import thread
import time
import subprocess
import tftutils
#
# MPC class
# maintains a thread listening for mpc changes
#
class MPC:
def __init__(self):
self.is_paused = False
self.volume = self.run_cmd('mpc volume')[7:10].strip()
self.change = tftutils.EventHook()
def start(self):
self.stop_flag = False
self.startListener()
def stop(self):
self.stop_flag = True
def run_cmd(self,cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output = p.communicate()[0]
return output
def seek(self,forward):
if forward:
self.run_cmd('mpc seek +5%')
else:
self.run_cmd('mpc seek -5%')
def set_volume(self,up):
if up:
self.run_cmd( 'mpc volume +2' )
else:
self.run_cmd( 'mpc volume -2' )
def get_volume(self):
return self.volume
def toggle_pause(self):
if self.is_paused == 1:
self.run_cmd('mpc play')
else:
self.run_cmd('mpc pause')
def load_stream(self, url):
self.run_cmd('mpc clear')
self.run_cmd('mpc add ' + url)
self.run_cmd('mpc play')
self.run_cmd('mpc current')
def play_track(self,url):
self.run_cmd('mpc clear')
self.run_cmd('mpc add ' + url)
self.run_cmd('mpc play')
def startListener(self):
try:
self.thread = thread.start_new_thread( self.get_stats, ())
return thread
except:
print "Error: MPC unable to startListener"
def get_stats(self):
while 1:
change = self.run_cmd('mpc idle player mixer')
if change.strip() == 'player':
current = self.run_cmd( 'mpc current')
status = self.run_cmd('mpc status')
lines = status.splitlines()
pause_change = False
if len(lines) > 1:
line_two_status = lines[1][lines[1].find('[')+1:lines[1].find(']')]
if line_two_status == 'paused' and self.is_paused == False:
self.is_paused = True
pause_change = True
elif line_two_status == 'playing' and self.is_paused == True:
self.is_paused = False
pause_change = True
self.change.fire({'type' : 'player', 'data': [current,status,pause_change]})
elif change.strip() == 'mixer':
self.volume = self.run_cmd('mpc volume')[7:10].strip()
self.change.fire({'type':'mixer', 'data': [self.volume]})