-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKioskPlayer.py
60 lines (50 loc) · 1.44 KB
/
KioskPlayer.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
from gpiozero import Button
from signal import pause
import datetime
import time
import subprocess
class KioskPlayer:
def __init__(self):
self.GPIO_PIN = 17
self.MEDIA_PLAYER = "mplayer"
self.MEDIA_FILE = "long-media.mp3"
#
self._player = None
self._playCounter = 0
self._running = False
def play_file(self):
self._playCounter = self._playCounter + 1
self._print("Ready to play instance {}".format(self._playCounter))
self._player = subprocess.Popen([self.MEDIA_PLAYER, self.MEDIA_FILE], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def _print(self, message):
now = datetime.datetime.now()
print("[{}] {}".format(now, message))
def isPlayerRunning(self):
self._print("Checking if player running")
if self._player is None:
return False
self._print("Poll check")
return self._player.poll() == None
def stop_player(self):
self._print("Stopping player")
self._player.stdin.write("{}\n".format("q").encode('utf-8'))
self._player.stdin.flush()
def run(self):
self._running = True
button = Button(self.GPIO_PIN, bounce_time = 0.05)
while self._running:
button.when_pressed = self.play_stop
pause()
def say_hello(self, button):
self._print("Hello")
def play_stop(self, button):
if self is None :
return
if self.isPlayerRunning():
self.stop_player()
else:
self.play_file()
if __name__ == '__main__':
print("Starting...")
kioskPlayer = KioskPlayer()
kioskPlayer.run()