-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaPlayerPrototype.py
523 lines (382 loc) · 16.3 KB
/
MediaPlayerPrototype.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import wx
import wx.media
from pprint import pprint as pp2
import datetime
from MediaPlayerBrowserStartSupportv2 import ID3
from MediaPlayerLibrary import findMP3s
#----------------------------------------------------------------------
def findSongData():
"""finds all the file paths, and song tags, then add or loads it
to the database"""
#make a list of songs
#list_of_songs = ['C:\\Programming\\Python\\Project\\MusicPlayer\\10.mp3',
#'C:\\Programming\\Python\\Project\\MusicPlayer\\1.mp3']
#list_of_songs = findMP3s(r'./songs')
tags = []
for song in findMP3s(r"E:\Music\as i lay dying"):
songTag = ID3(song)
tags.append(songTag)
return tags
class MediaPlayerApp(wx.Frame):
def __init__(self, *args, **kwargs):
super(MediaPlayerApp, self).__init__(id=wx.ID_ANY, *args, **kwargs)
#self.song_path = r'e:\music'
#self.song_path = r'C:\Programming\Python\Project\MusicPlayer\10.mp3'
self.song = r'C:\Programming\Python\Project\MusicPlayer\10.mp3'
self.sizer_main = wx.GridBagSizer()
self.sizer_main.SetRows(3)
self.SetSizer(self.sizer_main)
#sets the frame's favico
self.setFavicon()
#make a list of songs
self.list_of_songs = findSongData()
#sets up the main screen
self.initPlayer()
self.initSongList()
self.initSongInfoPane()
#self.setSizerMainColors()
self.Layout()
#self.Show()
self.Fit()
#self.Bind(wx., lambda e:self.snglst_panel.Layout)
#----------------------------------------------------------------------
def initSongInfoPane(self):
"""fills the song info grid with title, band, album,
length, size etc"""
self.info_pnl = wx.Panel(self)
self.info_szr = wx.BoxSizer()
self.info_trk = wx.TextCtrl(self.info_pnl, value='Track Name')
self.info_szr.Add(self.info_trk)
self.info_alb = wx.TextCtrl(self.info_pnl, value='Album Name')
self.info_szr.Add(self.info_alb)
self.info_bnd = wx.TextCtrl(self.info_pnl, value='Artist Name')
self.info_szr.Add(self.info_bnd)
self.info_pnl.SetSizer(self.info_szr)
self.info_szr.Layout()
self.sizer_main.Add(self.info_pnl, (1, 1),
flag=wx.EXPAND)
self.sizer_main.Layout()
self.Fit()
self.info_pnl.Show()
#----------------------------------------------------------------------
def setFavicon(self, path=None):
'''Sets the favicon for the app'''
if not path:
name = r'.\favicon1.ico'
type = wx.BITMAP_TYPE_ICO
favicon = wx.Icon(name, type, ) # 16, 16)
self.SetIcon(favicon)
#----------------------------------------------------------------------
def setSizerMainColors(self):
"""sets colors of the grids so I can debug a bit easier
row/column"""
panel = wx.Panel(self)
panel.SetBackgroundColour(wx.RED)
panel.Show()
self.sizer_main.Add(panel, (1, 1), flag=wx.EXPAND)
panel = wx.Panel(self)
#panel.SetBackgroundColour(wx.BLUE)
panel.Show()
self.sizer_main.Add(panel, (2, 1), flag=wx.EXPAND)
panel = wx.Panel(self)
panel.SetBackgroundColour(wx.GREEN)
panel.Show()
self.sizer_main.Add(panel, (1, 0), flag=wx.EXPAND)
panel = wx.Panel(self)
panel.SetBackgroundColour(wx.WHITE)
panel.Show()
self.sizer_main.Add(panel, (2, 0), flag=wx.EXPAND)
def initPlayer(self):
#create panel
self.player_panel = wx.Panel(self)
#self.player_panel.SetBackgroundColour(wx.BLUE)
#sizer for the panel
self.player_sizer = wx.BoxSizer(wx.VERTICAL)
self.player_panel.SetSizer(self.player_sizer)
#create labels for music picking
self.lbl_cursong = wx.StaticText(self.player_panel, label='Current Song:')
try:
lbl = self.song_path
except AttributeError as e:
lbl = '--SONG NOT SET--'
self.lbl_setsong = wx.StaticText(self.player_panel, label=lbl)
self.lbl_setsong.SetBackgroundColour((100, 90, 70))
self.btn_chooseSong = wx.Button(self.player_panel, label='Choose Song to play')
self.btn_chooseSong.SetBackgroundColour(wx.GREEN)
self.Bind(wx.EVT_BUTTON, self.media_path, self.btn_chooseSong)
#sizers
self.siz_songInfo = wx.BoxSizer(wx.HORIZONTAL)
self.siz_songInfo.AddMany([self.lbl_cursong, self.lbl_setsong])
self.siz_songChoose = wx.BoxSizer(wx.HORIZONTAL)
#self.siz_songChoose.Add(self.btn_chooseSong)
#main sizers for this section
self.player_sizer.AddMany([self.siz_songInfo, self.siz_songChoose])
self.btn_sizer = wx.GridBagSizer()
self.btn_sizer.Add(self.btn_chooseSong, (0, 1))
self.player_sizer.Add(self.btn_sizer)
#music player change the back end to WMP10, and catch the event for playback
self.mediaPlayer = wx.media.MediaCtrl(self.player_panel,
szBackend=wx.media.MEDIABACKEND_WMP10, )
self.Bind(wx.media.EVT_MEDIA_LOADED, self.media_loaded)
self.btn_sizer.Add(self.mediaPlayer, (0, 0))
self.mediaPlayer.SetVolume(1)
#play button
self.btn_play = wx.Button(self.player_panel, label='Play Song')
self.btn_play.SetBackgroundColour(wx.RED)
self.btn_sizer.Add(self.btn_play, (1, 0))
self.Bind(wx.EVT_BUTTON,
lambda e: self.media_play(self.song, e),
self.btn_play)
#pause button
self.btn_pause = wx.Button(self.player_panel, label='Pause')
self.btn_pause.SetBackgroundColour(wx.RED)
self.Bind(wx.EVT_BUTTON, self.media_pause, self.btn_pause)
self.btn_sizer.Add(self.btn_pause, (1, 1))
#next button
self.btn_next = wx.Button(self.player_panel, label='Next')
self.btn_next.SetBackgroundColour(wx.RED)
self.Bind(wx.EVT_BUTTON, self.media_next, self.btn_next)
self.btn_sizer.Add(self.btn_next, (1, 2))
#prev button
self.btn_prev = wx.Button(self.player_panel, label='Previous')
self.btn_prev.SetBackgroundColour('plum')
self.Bind(wx.EVT_BUTTON, self.media_prev, self.btn_prev)
self.btn_sizer.Add(self.btn_prev, (1, 3))
self.init_progress_bar()
#volume slider
self.sldr_vol = wx.Slider(self.player_panel, style=wx.VERTICAL | wx.SL_INVERSE)
self.sldr_vol.SetMin(0)
self.sldr_vol.SetMax(100)
volume = self.mediaPlayer.GetVolume()
self.sldr_vol.SetValue(1000)
self.Bind(wx.EVT_SLIDER, self.SetVol, self.sldr_vol)
self.btn_sizer.Add(self.sldr_vol, (2, 0))
self.sizer_main.Add(self.player_panel, (0, 1),
flag=wx.EXPAND)
self.btn_sizer.Layout()
self.player_sizer.Layout()
self.player_panel.Fit()
self.Show()
self.Layout()
#slider updater
print 'timer created'
#self.player_timer = NewTimer(owner=self)
self.player_timer = wx.Timer(owner=self)
print 'timer set ?:', self.player_timer.Start(1000)
self.Bind(wx.EVT_TIMER, self.onTimer)
print 'timer done binded'
#----------------------------------------------------------------------
def init_progress_bar(self):
""""""
#progress sizer
self.szr_prog = wx.GridBagSizer()
#progress label title
self.lbl_prog_title = wx.StaticText(self.player_panel)
self.lbl_prog_title.SetLabel('Progress Slider')
self.szr_prog.Add(self.lbl_prog_title, (1, 3))
#progress label current pos
self.lbl_prog_cur = wx.StaticText(self.player_panel)
self.lbl_prog_cur.SetLabel('Current Time')
self.szr_prog.Add(self.lbl_prog_cur, (1, 4))
#start label
self.lbl_start = wx.StaticText(self.player_panel)
self.lbl_start.SetLabel('0:00')
self.szr_prog.Add(self.lbl_start, (1, 0))
#end label
self.lbl_end = wx.StaticText(self.player_panel)
self.lbl_end.SetLabel('9:99')
self.szr_prog.Add(self.lbl_end, (1, 6), )
#progress slider
self.sldr_prog = wx.Slider(self.player_panel)
self.Bind(wx.EVT_SLIDER, self.SetProg, self.sldr_prog)
self.sldr_prog.SetMinSize((200, -1))
self.szr_prog.Add(self.sldr_prog, (0, 0), span=(1, 3))
self.btn_sizer.Add(self.szr_prog, (3, 0))
#----------------------------------------------------------------------
def media_next(self, e):
""""""
print 'does nothing'
pass
#----------------------------------------------------------------------
def media_prev(self, e):
""""""
print 'does nothing'
pass
#----------------------------------------------------------------------
def onTimer(self, e):
"""Gets called when the player_timer goes off every second, in order
to move the progress slider and other update-related things"""
#current value of the song's progress
current = self.mediaPlayer.Tell()
#print 'current', current
self.sldr_prog.SetValue(current)
#current volue
volume = self.mediaPlayer.GetVolume()
#self.SetVol(100)
#resize progress slider
xsize = self.player_panel.Size[0]
ysize = -1
self.sldr_prog.SetSize((xsize, ysize))
#progress labels, current time, total song length
self.setProgLabel(current)
length = self.mediaPlayer.Length()
end_time = self.toTimeString(length)
self.lbl_end.SetLabel(end_time)
def setProgLabel(self, ms, text=None):
if not text:
template = 'current time: '
text = self.toTimeString(ms)
self.lbl_prog_cur.SetLabel(template+text)
# self.lbl_prog_title.GetLabelText()
else:
self.lbl_prog_cur.SetLabel(template+text)
def toTimeString(self, ms):
'''Takes milliseconds, converts it to hh:mm:ss'''
if ms != '-1':
s = datetime.timedelta(milliseconds=ms)
hours, remainder = divmod(s.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
hours = str(hours).zfill(2)
minutes = str(minutes).zfill(2)
seconds = str(seconds).zfill(2)
if int(hours):
time_string = '%s:%s:%s' % (hours, minutes, seconds)
else:
time_string = '%s:%s' % (minutes, seconds)
return time_string
else:
return 'not set'
#----------------------------------------------------------------------
def SetVol(self, e):
"""sets the volume of the song, in percentage of 100"""
vol = self.sldr_vol.GetValue()
#print 'cur vol val:', vol
#print 'volume to be set:', vol
vol /= 100.0
self.mediaPlayer.SetVolume(vol)
#----------------------------------------------------------------------
def SetProg(self, e):
"""seeks the song to wherever the slider stops"""
prog = self.sldr_prog.GetValue()
print 'prog to seek to:', prog
self.mediaPlayer.Seek(prog)
self.onTimer(e)
def media_loaded(self, e):
#print 'caught EVT_MEDIA_LOADED'
self.mediaPlayer.Play()
#progress slider
maxLen = self.mediaPlayer.Length()
#print 'maxlen', maxLen, 'sec'
self.sldr_prog.SetRange(0, maxLen)
#volume slider
self.sldr_vol.SetRange(0, 100)
#----------------------------------------------------------------------
def media_path(self, e):
""""""
print 'Gotcha bitch'
dlg = wx.FileDialog(self, )
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
print 'path chosen:', path
self.lbl_setsong.SetLabel(path)
self.Layout()
self.song_path = path
def media_play(self, path, event=None):
'''Plays the media, at path'''
if type(path) != str:
path = path.fp
print 'load:', path
self.mediaPlayer.Load(path)
self.mediaPlayer.SetInitialSize()
self.mediaPlayer.SetVolume(1)
self.mediaPlayer.SetPlaybackRate(1)
print 'music playing'
def media_pause(self, event):
'''Pauses the media. If it's already paused, it'll start it up'''
if not self.mediaPlayer.GetState() == 1:
self.mediaPlayer.Pause()
else:
self.mediaPlayer.Play()
#----------------------------------------------------------------------
def initSongList(self):
"""builds a list of songs in the current dir"""
self.snglst_sizer = wx.BoxSizer()
self.snglst_panel = wx.Panel(self)
self.sngLst = wx.ListCtrl(self.snglst_panel, style=wx.LC_REPORT,
size=(500, 300))
#create col headers
self.sngLst.InsertColumn(0, 'Track')
self.sngLst.InsertColumn(1, 'Album')
self.sngLst.InsertColumn(2, 'Band')
#set column widths manually
self.sngLst.SetColumnWidth(0, 150)
self.sngLst.SetColumnWidth(1, 150)
self.sngLst.SetColumnWidth(2, 150)
##set col widths automatically
#self.sngLst.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
#self.sngLst.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
#self.sngLst.SetColumnWidth(2, wx.LIST_AUTOSIZE_USEHEADER)
#create the sizer for the song list
self.snglst_sizer.Add(self.sngLst)
self.snglst_sizer.Layout()
self.snglst_panel.Show()
#add the songist to the main sizer
self.sizer_main.Add(self.snglst_panel, (0, 0),
#span=wx.GBSpan(3, 1),
flag=wx.EXPAND)
self.rowData = {}
for i, song in enumerate(self.list_of_songs):
songTag = song
self.sngLst.InsertStringItem(i, songTag.title)
self.sngLst.SetStringItem(i, 1, songTag.album.decode(errors='ignore'))
self.sngLst.SetStringItem(i, 2, songTag.artist.decode(errors='ignore'))
self.sngLst.SetItemData(i, songTag.id)
print songTag.title, '\t', songTag.artist, '\t', songTag.album
self.rowData[songTag.id] = songTag
#bind item select to play a song
self.sngLst.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onSongSelect)
self.sngLst.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onSongActive)
self.Fit()
self.Layout()
#----------------------------------------------------------------------
def onSongActive(self, e):
"""print the song info"""
##
index = e.m_itemIndex
self.song = self.rowData[self.sngLst.GetItemData(index)]
#print song.fp
print 'playing {}'.format(self.song)
self.media_play(self.song.fp)
self.onTimer(e)
#----------------------------------------------------------------------
def onSongSelect(self, e):
"""print the song info"""
index = e.m_itemIndex
song = self.rowData[self.sngLst.GetItemData(index)]
print song.title, song.album, song.artist
try:
self.info_trk.SetValue(song.title)
self.info_alb.SetValue(song.album)
self.info_bnd.SetValue(song.artist)
except AttributeError as e:
pass
print 'song select error', e
########################################################################
class NewTimer(wx.Timer):
"""override wx.timer"""
#----------------------------------------------------------------------
def __init__(self, owner, *args, **kwargs):
"""Constructor"""
super(wx.Timer, self).__init__(owner=owner, *args, **kwargs)
self.Start(1000)
print self.IsRunning()
#----------------------------------------------------------------------
def Notify(self):
"""updates slider progress"""
print 'caught notify'
def main():
ex = wx.App(redirect=False) #redirect to wxStdOut
player = MediaPlayerApp(None, title='TEST')
ex.MainLoop()
if __name__ == '__main__':
main()