-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
360 lines (260 loc) · 14.1 KB
/
player.py
File metadata and controls
360 lines (260 loc) · 14.1 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
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
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version 3.10.1-0-g8feb16b)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class playerWindow
###########################################################################
class playerWindow ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"MINEATORY PLAYER", pos = wx.DefaultPosition, size = wx.Size( 557,381 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHTTEXT ) )
self.player_menubar = wx.MenuBar( 0 )
self.menu_file = wx.Menu()
self.menu_file_openLibrary = wx.MenuItem( self.menu_file, wx.ID_ANY, u"Open Library", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_file.Append( self.menu_file_openLibrary )
self.menu_file_open = wx.MenuItem( self.menu_file, wx.ID_ANY, u"Open a media file...", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_file.Append( self.menu_file_open )
self.menu_file.AppendSeparator()
self.menu_file_about = wx.MenuItem( self.menu_file, wx.ID_ANY, u"About Mineatory Player...", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_file.Append( self.menu_file_about )
self.menu_file.AppendSeparator()
self.menu_file_preferences = wx.MenuItem( self.menu_file, wx.ID_ANY, u"Preferences", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_file.Append( self.menu_file_preferences )
self.menu_file.AppendSeparator()
self.menu_file_exit = wx.MenuItem( self.menu_file, wx.ID_ANY, u"Exit Mineatory Player...", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_file.Append( self.menu_file_exit )
self.player_menubar.Append( self.menu_file, u"File" )
self.menu_source = wx.Menu()
self.menu_source_update = wx.MenuItem( self.menu_source, wx.ID_ANY, u"Update source from Mineatory Server", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_source.Append( self.menu_source_update )
self.menu_source_clear = wx.MenuItem( self.menu_source, wx.ID_ANY, u"Clear my Source", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_source.Append( self.menu_source_clear )
self.menu_source.AppendSeparator()
self.menu_source_download = wx.MenuItem( self.menu_source, wx.ID_ANY, u"Download songs from Mineatory Server", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_source.Append( self.menu_source_download )
self.menu_source_manager = wx.MenuItem( self.menu_source, wx.ID_ANY, u"Open Source Manager", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_source.Append( self.menu_source_manager )
self.player_menubar.Append( self.menu_source, u"Source" )
self.menu_play = wx.Menu()
self.menu_play_pause = wx.MenuItem( self.menu_play, wx.ID_ANY, u"Pause / Play (Space)", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_play.Append( self.menu_play_pause )
self.menu_play_end = wx.MenuItem( self.menu_play, wx.ID_ANY, u"End this song", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_play.Append( self.menu_play_end )
self.menu_play.AppendSeparator()
self.menu_play_front = wx.MenuItem( self.menu_play, wx.ID_ANY, u"Jump 5 seconds front", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_play.Append( self.menu_play_front )
self.menu_play_back = wx.MenuItem( self.menu_play, wx.ID_ANY, u"Jump 5 seconds back", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_play.Append( self.menu_play_back )
self.menu_play.AppendSeparator()
self.menu_play_info = wx.MenuItem( self.menu_play, wx.ID_ANY, u"Display song info...", wx.EmptyString, wx.ITEM_NORMAL )
self.menu_play.Append( self.menu_play_info )
self.player_menubar.Append( self.menu_play, u"Play" )
self.SetMenuBar( self.player_menubar )
gSizer1 = wx.GridSizer( 0, 2, 0, 0 )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"Playlist", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
bSizer1.Add( self.m_staticText1, 0, wx.ALL, 5 )
list_playListChoices = []
self.list_playList = wx.ListBox( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, list_playListChoices, 0 )
bSizer1.Add( self.list_playList, 1, wx.ALL|wx.EXPAND, 5 )
gSizer3 = wx.GridSizer( 0, 2, 0, 0 )
self.btn_addSong = wx.Button( self, wx.ID_ANY, u"Add songs...", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer3.Add( self.btn_addSong, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )
self.btn_deleteSong = wx.Button( self, wx.ID_ANY, u"Delete Selected", wx.DefaultPosition, wx.DefaultSize, 0 )
gSizer3.Add( self.btn_deleteSong, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 5 )
bSizer1.Add( gSizer3, 0, wx.EXPAND, 5 )
gSizer1.Add( bSizer1, 0, wx.EXPAND, 5 )
btn_list_add = wx.BoxSizer( wx.VERTICAL )
self.m_bitmap1 = wx.StaticBitmap( self, wx.ID_ANY, wx.Bitmap( u"pic/music_albums_fill.bmp", wx.BITMAP_TYPE_ANY ), wx.DefaultPosition, wx.DefaultSize, 0 )
btn_list_add.Add( self.m_bitmap1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
self.label_songTitle = wx.StaticText( self, wx.ID_ANY, u"Not Playing Right now...", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTER_HORIZONTAL )
self.label_songTitle.Wrap( -1 )
self.label_songTitle.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, wx.EmptyString ) )
btn_list_add.Add( self.label_songTitle, 0, wx.ALL|wx.EXPAND, 5 )
self.slider_progressDisplay = wx.Slider( self, wx.ID_ANY, 0, 0, 100, wx.DefaultPosition, wx.DefaultSize, wx.SL_HORIZONTAL )
btn_list_add.Add( self.slider_progressDisplay, 0, wx.ALL|wx.EXPAND, 5 )
wSizer1 = wx.WrapSizer( wx.HORIZONTAL, wx.WRAPSIZER_DEFAULT_FLAGS )
self.btn_back = wx.BitmapButton( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size( 50,50 ), wx.BU_AUTODRAW|0 )
self.btn_back.SetBitmap( wx.Bitmap( u"pic/angle-double-left (1).bmp", wx.BITMAP_TYPE_ANY ) )
wSizer1.Add( self.btn_back, 0, wx.ALL, 5 )
self.btn_play = wx.BitmapButton( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size( 50,50 ), wx.BU_AUTODRAW|0 )
self.btn_play.SetBitmap( wx.Bitmap( u"pic/play circle.bmp", wx.BITMAP_TYPE_ANY ) )
wSizer1.Add( self.btn_play, 0, wx.ALL, 5 )
self.btn_pause = wx.BitmapButton( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size( 50,50 ), wx.BU_AUTODRAW|0 )
self.btn_pause.SetBitmap( wx.Bitmap( u"pic/pause circle.bmp", wx.BITMAP_TYPE_ANY ) )
wSizer1.Add( self.btn_pause, 0, wx.ALL, 5 )
self.btn_front = wx.BitmapButton( self, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size( 50,50 ), wx.BU_AUTODRAW|0 )
self.btn_front.SetBitmap( wx.Bitmap( u"pic/angle-double-right.bmp", wx.BITMAP_TYPE_ANY ) )
wSizer1.Add( self.btn_front, 0, wx.ALL, 5 )
btn_list_add.Add( wSizer1, 0, wx.ALIGN_CENTER_HORIZONTAL, 5 )
gSizer4 = wx.GridSizer( 0, 2, 0, 0 )
self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, u"Volume", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTER_HORIZONTAL )
self.m_staticText3.Wrap( -1 )
gSizer4.Add( self.m_staticText3, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 )
self.slider_volume = wx.Slider( self, wx.ID_ANY, 50, 0, 100, wx.DefaultPosition, wx.DefaultSize, wx.SL_HORIZONTAL )
gSizer4.Add( self.slider_volume, 1, wx.ALL|wx.EXPAND, 5 )
btn_list_add.Add( gSizer4, 0, wx.EXPAND, 5 )
gSizer1.Add( btn_list_add, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 5 )
self.SetSizer( gSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Bind( wx.EVT_MENU, self.openAbout, id = self.menu_file_about.GetId() )
self.btn_addSong.Bind( wx.EVT_BUTTON, self.openMusicFile )
self.list_playList.Bind( wx.EVT_ENTER_WINDOW, self.refreshQuene )
self.btn_deleteSong.Bind( wx.EVT_BUTTON, self.deleteSelectedSong )
self.Bind( wx.EVT_MENU, self.openMusicFile, id = self.menu_file_open.GetId() )
self.list_playList.Bind( wx.EVT_LISTBOX_DCLICK, self.selectAndPlayFile )
def __del__( self ):
pass
# Virtual event handlers, override them in your derived class
def openAbout( self, event ):
event.Skip()
def openMusicFile( self, event ):
event.Skip()
def refreshQuene( self, event ):
event.Skip()
def deleteSelectedSong( self, event ):
event.Skip()
def selectAndPlayFile( self, event ):
event.Skip()
import os
import pydub
import pygame
import _thread
from pydub.playback import play
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
class Player():
def __init__(self):
self.target_media_file = None # Originally opened file in Python
self.target_media_file_location = "" # File location in string
self.target_file_type = "" # Recorded file extension
self.playing_file = None # File for Pydub
# Basic functions
def getFileExtension(self, target_file_location:str):
extensionName:str = ""
# For loop into every characters of the string
fileExtensionStartLocation = target_file_location.find(".")
extensionName = target_file_location[fileExtensionStartLocation+1:]
print(extensionName)
return extensionName
# Non Basic functions
def openFile(self, file_location:str, uploadToPydub:bool = True):
"Open the target file in the file location of this class. System will automatically override the current file"
# Apply the file location
self.target_media_file_location = file_location
# Identify the file extenstion
self.target_file_type = self.getFileExtension(self.target_media_file_location)
# Open the file and apply to a variable
self.target_media_file = open(self.target_media_file_location, "rb")
# Upload the file to Pydub
if (uploadToPydub == True):
self.playing_file = pydub.AudioSegment.from_file(self.target_media_file_location, format=self.target_file_type)
print("The target file has already opened")
print(self.target_media_file)
print(self.target_media_file_location)
print(self.target_file_type)
print(self.playing_file)
def closeFile(self):
"Close the target file in the class."
# Close and reset the identities
self.target_media_file.close()
self.target_media_file_location = ""
self.target_file_type = ""
def playMusicFromFile(self):
"Start the play the music from the file in the class"
# Open the file with pydub
play(self.playing_file)
# Set the title to the file name
# self.label_songTitle.SetLabel(os.path.basename(self.target_media_file_location))
print("The file is playing")
return
class PageEvent(playerWindow):
def __init__(self, parent):
playerWindow.__init__(self, parent)
self.Player = Player() #Instantiate
self.playQuene = []
# Basic functions
def setPlayingTitle(self, title:str):
self.label_songTitle.SetLabel(title)
def openAbout(self, event):
try:
os.system("python about.py")
return
except:
os.system("python3 about.py")
return
def openMusicFile(self, event):
fileLocation = filedialog.askopenfilename(filetypes=[("MP3 Media File", "*.mp3"), ("WAV Media File", "*.wav")])
fileExtention = self.Player.getFileExtension(fileLocation)
# Generate a metadata for songs in quene and add to the list
song_metadata = {
"name": os.path.basename(fileLocation),
"location": fileLocation
}
# check whether this item has already appended into the quene
for i in self.playQuene:
if (i["name"] == song_metadata["name"]):
result = messagebox.askyesno("Song has already existed", "The song you are going to append has already added to this quene. Do you want to continue?")
if (result == True):
break
else:
return
# add this metadata to the list
self.playQuene.append(song_metadata)
# try:
# except:
# messagebox.showerror("Open file error", "Mineatory cannot open the file that you specific.")
def refreshQuene(self, event):
self.list_playList.Clear()
for i in self.playQuene:
self.list_playList.Append(i["name"])
def deleteSelectedSong(self, event):
selected_song_name = self.list_playList.GetStringSelection()
print(selected_song_name)
# Identify whether there is a selection from the user
if (selected_song_name == "" or selected_song_name == None):
messagebox.showwarning("Select a song to continue...", "Please select a song to continue removing.")
return
# index the quene and delete the one match the name ONCE
for i in self.playQuene:
if (i["name"] == selected_song_name):
# find the index of the value
target_index = self.playQuene.index(i)
self.playQuene.pop(target_index)
break
else:
continue
def selectAndPlayFile(self, event):
# Gather the selection from the list
selected_song_name = self.list_playList.GetStringSelection()
# Find the file location of that target file and open
selected_song_location = ""
for i in self.playQuene:
if (i["name"] == selected_song_name):
selected_song_location = i["location"]
break
else:
continue
# Open the file
self.Player.openFile(file_location=selected_song_location, uploadToPydub=True)
# Create a thread to play music
player_thread_num = _thread.start_new_thread(self.Player.playMusicFromFile, ())
# self.Player.playMusicFromFile()
if (__name__ == "__main__"):
root = Tk()
root.withdraw()
app = wx.App(False)
page = PageEvent(None)
page.Show(True)
app.MainLoop()