forked from masapasa/VoiceBotChatGPT-RaspberryPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_player.py
59 lines (54 loc) · 1.88 KB
/
radio_player.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
import vlc
import threading
import time
class RadioPlayer:
def __init__(self, wakeword_detector):
self.wakeword_detector = wakeword_detector
self.player = vlc.MediaPlayer()
self.thread = None
self.running = False
self.stream_url = None
self.blink_led_thread = None
def start(self, stream_url=None):
if not self.running:
if stream_url is None:
stream_url = self.stream_url
else:
self.stream_url = stream_url
if stream_url is None:
print("No stream URL provided")
return
self.running = True
self.wakeword_detector.is_awoken = True
self.blink_led_thread = threading.Thread(target=self.blink_led)
self.blink_led_thread.start()
self.player.set_media(vlc.Media(stream_url))
self.thread = threading.Thread(target=self._play)
self.thread.start()
def blink_led(self):
while self.running:
self.wakeword_detector.handle_led_event("Paused")
time.sleep(0.5)
self.wakeword_detector.handle_led_event("Off")
time.sleep(0.5)
def _play(self):
try:
self.player.play()
while self.running:
time.sleep(1)
except Exception as e:
print(f"Error playing stream: {e}")
self.stop()
def stop(self):
if self.running:
self.running = False
self.wakeword_detector.is_awoken = False
self.player.stop()
if self.blink_led_thread is not None and self.blink_led_thread.is_alive():
self.blink_led_thread.join()
if self.thread:
self.thread.join()
self.thread = None
def cleanup(self):
self.stop()
self.wakeword_detector = None