-
Notifications
You must be signed in to change notification settings - Fork 1
/
playback_master.py
86 lines (78 loc) · 2.17 KB
/
playback_master.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
import requests as http
from lib.mouse import Mouse
from lib.GPIOInteraction import GPIOInteractor
from lib.SoundPlayer import SoundPlayer
import os
import time
os.environ['PLAYING'] = 'False'
playing = False
pause_time = 0
paused_at = time.time()
pressed_at = time.time()
skip = False
playback_url = 'http://127.0.0.1:5000/playback?state='
# called whenever the physical pause/play button is pressed
def toggle_play_pause():
"""Pauses or Resumes music from playback master."""
global playing
global paused_at
global pressed_at
global pause_time
if(m.in_transition or time.time()-pressed_at < 1):
return None
pressed_at = time.time()
if(playing):
m.pause()
http.get(playback_url + 'paused')
playing = False
paused_at = time.time()
s.play_pause_tone()
elif(not playing):
m.play()
http.get(playback_url + 'resume')
playing = True
pause_time += time.time() - paused_at
def skip_song():
global skip
skip = True
s.play_skip_tone()
m = Mouse()
g = GPIOInteractor()
s = SoundPlayer()
g.set_button_callback(toggle_play_pause)
g.set_button_held_callback(skip_song)
song_url = 'http://127.0.0.1:5000/get_next_song'
s.play_boot_tone()
def get_next_song():
"""Gets next song and returns data"""
try:
response = http.get(song_url)
except http.exceptions.ConnectionError:
print('unable to connect, waiting...')
time.sleep(10)
return get_next_song()
data = response.json()
while 'error' in data:
print(data['error'])
time.sleep(10)
response = http.get(song_url)
data = response.json()
return data
# establish a connection
data = get_next_song()
duration = data['duration'] / 1000.0
start_time = time.time()
m.play_song(data['track_id'])
playing = True
# main loop
while True:
if time.time() - start_time + 3>= duration + pause_time or skip:
if(playing):
toggle_play_pause()
data = get_next_song()
duration = data['duration'] / 1000.0
m.play_song(data['track_id'])
start_time = time.time()
pause_time = 0
skip = False
playing = True