-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfiler_musicplayer.py
231 lines (180 loc) · 7.27 KB
/
cfiler_musicplayer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import ctypes
import configparser
import ckit
import cfiler_mainwindow
import cfiler_filelist
import cfiler_statusbar
#--------------------------------------------------------------------
class SongMCI:
def __init__( self, filename ):
self.filename = filename
ret = ctypes.windll.winmm.mciSendStringW( 'open "%s" alias %d' % ( self.filename, id(self) ), None, 0, None )
ret = ctypes.windll.winmm.mciSendStringW( 'set %d time format milliseconds' % ( id(self), ), None, 0, None )
def __del__(self):
self.close()
def play(self):
ret = ctypes.windll.winmm.mciSendStringW( 'play %d' % ( id(self), ), None, 0, None )
def isPlaying(self):
buf = ctypes.create_unicode_buffer(32)
ret = ctypes.windll.winmm.mciSendStringW( 'status %d mode' % ( id(self), ), buf, len(buf), None )
if buf.value == 'playing':
return True
else:
return False
def stop(self):
ret = ctypes.windll.winmm.mciSendStringW( 'stop %d' % ( id(self), ), None, 0, None )
def seek( self, pos ):
value = int( pos * 1000 )
if self.isPlaying():
ret = ctypes.windll.winmm.mciSendStringW( 'play %d from %d' % ( id(self), value ), None, 0, None )
def length(self):
buf = ctypes.create_unicode_buffer(32)
ret = ctypes.windll.winmm.mciSendStringW( 'status %d length' % ( id(self), ), buf, len(buf), None )
if ret : return 0.0
return int(buf.value)/1000.0
def position(self):
buf = ctypes.create_unicode_buffer(32)
ret = ctypes.windll.winmm.mciSendStringW( 'status %d position' % ( id(self), ), buf, len(buf), None )
if ret : return 0.0
return int(buf.value)/1000.0
def close(self):
ret = ctypes.windll.winmm.mciSendStringW( 'close %d' % ( id(self), ), None, 0, None )
class MusicPlayer:
def __init__( self, main_window ):
self.main_window = main_window
self.items = []
self.cursor = 0
self.song = None
self.playing = False
self.song_name = ""
self.position = None
self.length = None
self.status_bar = MusicPlayerStatusBar(self)
self.main_window.statusBar().registerLayer(self.status_bar)
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
self.main_window.setTimer( self.onTimer, 10 )
self.main_window.setTimer( self.onTimerStatusBar, 1000 )
def destroy(self):
self.stop()
self.main_window.killTimer( self.onTimer )
self.main_window.killTimer( self.onTimerStatusBar )
self.main_window.statusBar().unregisterLayer(self.status_bar)
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def setPlayList( self, items, selection ):
self.stop()
self.items = items
self.cursor = selection
self.song_name = self.items[self.cursor].name
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def getPlayList(self):
return ( self.items, self.cursor )
def save( self, ini, section ):
i=0
for item in self.items:
ini.set( section, "playlist_%d"%(i,), item.getFullpath() )
i+=1
while True:
if not ini.remove_option( section, "playlist_%d"%(i,) ) : break
i+=1
ini.set( section, "track", str(self.cursor) )
ini.set( section, "position", str(int(self.position)) )
def load( self, ini, section ):
for i in range(100):
try:
path =ckit.normPath(ini.get( section, "playlist_%d"%(i,) ))
location, filename = os.path.split(path)
item = cfiler_filelist.item_Default(
location,
filename
)
self.items.append(item)
except configparser.NoOptionError:
break
if len(self.items)==0 : return
self.cursor = ini.getint( section, "track" )
self.position = ini.getint( section, "position" )
self.cursor = min( self.cursor, len(self.items)-1 )
self.song_name = self.items[self.cursor].name
self.play()
self.seek(self.position)
self.pause()
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def play(self):
self.song = SongMCI( self.items[self.cursor].getFullpath() )
self.song.play()
self.playing = True
def stop(self):
if self.song:
self.song.close()
self.song = None
self.playing = False
def pause(self):
if self.song:
if self.song.isPlaying():
self.song.stop()
self.playing = False
else:
self.song.play()
self.playing = True
def seek(self,pos):
if self.song:
self.song.seek(pos)
def advance( self, delta ):
if self.song:
p = self.song.position()
t = self.song.length()
p += delta
p = min( p, t )
p = max( p, 0.0 )
self.song.seek(p)
def prev(self):
if self.cursor-1 >= 0:
self.cursor -= 1
self.song_name = self.items[self.cursor].name
self.play()
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def next(self):
if self.cursor+1 < len(self.items):
self.cursor += 1
self.song_name = self.items[self.cursor].name
self.play()
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def select( self, sel ):
if sel < len(self.items):
self.cursor = sel
self.song_name = self.items[sel].name
self.play()
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def isPlaying(self):
return self.playing
def onTimer(self):
if self.song:
if not self.song.isPlaying():
if self.playing:
self.next()
self.position = self.song.position()
self.length = self.song.length()
else:
self.position = None
self.length = None
def onTimerStatusBar(self):
self.main_window.paint(cfiler_mainwindow.PAINT_STATUS_BAR)
def _timeString(t):
m = int( t // (60) )
t -= m * (60)
s = int( t )
return "%d:%02d" % (m,s)
class MusicPlayerStatusBar( cfiler_statusbar.StatusBarLayer ):
def __init__( self, music_player ):
cfiler_statusbar.StatusBarLayer.__init__( self, 1.0 )
self.music_player = music_player
def paint( self, window, x, y, width, height ):
if self.music_player.position!=None and self.music_player.length!=None:
right = " %s - %s " % ( _timeString(self.music_player.position), _timeString(self.music_player.length-self.music_player.position) )
else:
right = " "
left = " [ Music %d/%d ] %s" % ( self.music_player.cursor+1, len(self.music_player.items), self.music_player.song_name )
left = ckit.adjustStringWidth( window, left, width-len(right), ckit.ALIGN_LEFT, ckit.ELLIPSIS_RIGHT )
attr = ckit.Attribute( fg=ckit.getColor("bar_fg"))
window.putString( x, y, width, y, attr, left+right )