forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
60 lines (55 loc) · 1.71 KB
/
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
60
from utils import *
try:
PlayerEventCallbacks
except NameError:
# This is fixed. In case we add some, we have to restart.
class PlayerEventCallbacks:
onSongChange = None
onSongFinished = None
onPlayingStateChange = None
def songs(state):
if state.curSong:
# We just started the player and we have a current song from persistent storage.
# Yield it now so that we begin playing with this song.
# Yield the Song object itself, though, not the ObjectProxy. The ObjectProxy
# would result in very strange behavior later for onSongChange events.
song = state.curSong.__get__(None)
assert song
song.openFile()
yield song
import queue
while True:
song = queue.getNextSong()
assert song
song.openFile()
yield song
# This is an special extra callback.
# This is called very first. We do this so that
# we always have state.curSong right.
def onSongChange(**kwargs):
from State import state
state.curSong = kwargs["newSong"]
def loadPlayer(state):
import ffmpeg
player = ffmpeg.createPlayer()
for e in [m for m in dir(PlayerEventCallbacks) if not m.startswith("_")]:
cb = EventCallback(targetQueue=state.updates, name=e)
if e == "onSongChange":
cb.extraCall = onSongChange
setattr(PlayerEventCallbacks, e, cb)
setattr(player, e, cb)
player.queue = songs(state)
player.volume = state.volume
return player
def playerMain():
from State import state
#state.player.playing = True
for ev,args,kwargs in state.updates.read():
if ev is PlayerEventCallbacks.onSongChange:
state.curSong.save()
oldSong = kwargs["oldSong"]
if oldSong:
if kwargs["skipped"]: oldSong.skipped = True
state.recentlyPlayedList.append(oldSong)
state.recentlyPlayedList.save()
pass # onPlayingStateChange