-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMplayer.py
More file actions
39 lines (30 loc) · 1.05 KB
/
Mplayer.py
File metadata and controls
39 lines (30 loc) · 1.05 KB
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
import time
from datetime import timedelta, datetime
from threading import Thread
import vlc
class Mplayer(object):
def __init__(self):
self.running = False
self.player = None
self.last_started = None
def start(self, song_file: str, duration: timedelta) -> None:
if self.running:
self.stop()
self.running = True
# start player
self.player = vlc.MediaPlayer(song_file, 'input-repeat=-1')
self.player.play()
# stop player later
stopper_thread = Thread(None, self.__stop_player, "Alarm stopper thread", (duration, self.last_started))
stopper_thread.start()
def __stop_player(self, duration: timedelta, started: datetime) -> None:
for slept in range(0, int(duration.total_seconds())):
time.sleep(1)
if not self.running or not self.last_started == started:
return
self.stop()
def stop(self) -> None:
if not self.running:
return
self.player.stop()
self.running = False