-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyMediaPlayer (main).py
142 lines (115 loc) · 5.23 KB
/
PyMediaPlayer (main).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
## Started - 14-06-2020
## completed - 18-06-2020
# COPYRIGHT® 2020
# ——————————————
# PyMediaPlayer
# Made by "Charitra Agarwal"
# "www.youtube.com/c/EverythingComputerized"
# "www.linkedin.com/in/charitra1022/"
############ ###
############# #####
### ### ###
### ### ###
### ### ###
### ### ###
### ###############
### #################
### ### ###
### ### ###
############# ### ###
############ ### ###
# Instructions:-
# 1. Install libraries:- pyqt5, pyqt5-tools, stagger, pillow
# 2. Icons folder should be correctly placed
# Universal imports
import os
import sys
import tempfile
import shutil
from PyQt5.QtWidgets import QMainWindow
# Local imports
from PyMediaPlayerAbstract import MediaPlayer, hostOS
from SingleApplication import SingleApplicationWithMessaging, SingleApplication
# change the media plugin in Windows OS for better media support
# default plugin of Windows OS is 'DirectShow' released for XP which is outdated
# new plugin is 'Windows Media Foundation' released for and after Windows Vista
try:
if hostOS == "windows":
os.environ['QT_MULTIMEDIA_PREFERRED_PLUGINS'] = 'windowsmediafoundation'
# if hostOS == "linux": os.environ['QT_DEBUG_PLUGINS'] = "1"
# enable above statement if code runs into error
# helps in detecting the cause of error
except Exception as err:
print("Plugin Change Error:", err)
class MainWindow(QMainWindow, MediaPlayer):
"""Setup PyMediaPlayer UI, controls and functionalities"""
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
try:
self.setAcceptDrops(True) # making the window accept drag and drop
self.add_songs(sys.argv) # add songs from command-line args
self.show() # show the MainWindow as active
except Exception as err:
print("Error in MainWindow:", err)
def dragEnterEvent(self, e):
"""When a drag and drop event is made onto the window"""
try:
if e.mimeData().hasUrls():
e.acceptProposedAction()
except Exception as err:
print("Error in dragEnterEvent:", err)
def dropEvent(self, e):
"""When the user drops the object to the Window, process and filter them, and then add them to playlist"""
try:
songs = []
for url in e.mimeData().urls():
songs.append(url.toLocalFile()) # get the absolute path of all
self.add_songs(songs,) # add all songs to the playlist
except Exception as err:
print("Error in dropEvent:", err)
def accept_songs(self, message):
"""If another instance of app is running,
then take the song as message and send it as a list to
the already running instance
NOTE:-
Message is received as a string joined by ';'
So, first split it into a list
!! Make sure message is sent as a list, or else it wouldn't work"""
try:
message = message.split(';')
self.add_songs(message,)
except Exception as err:
print("Error in accept_songs:", err)
def closeEvent(self, e):
"""Called when user attempts to close the app.
Redefined for Cleanup all the temporary images before leaving."""
if hostOS == 'windows':
temp_dir = str(tempfile.gettempdir()) + '\\PyMediaPlayer\\'
if hostOS == 'linux':
temp_dir = str(tempfile.gettempdir()) + '/PyMediaPlayer/'
try:
shutil.rmtree(temp_dir, ignore_errors=True) # delete temporary files after closing
except Exception as err:
print("Error in closeEvent function:", err)
def main():
# Unique key of the app (Shouldn't match with other apps in the Host system !!!!!)
key = 'PYMEDIAPLAYER-PYQT5-CHARITRA-AGARWAL'
# send commandline args as message
if len(sys.argv) >= 1:
app = SingleApplicationWithMessaging(sys.argv, key)
if app.isRunning():
print('Sending parameters to already running instance of app and Exiting.')
app.sendMessage(';'.join(sys.argv))
else:
app = SingleApplication(sys.argv, key)
if app.isRunning():
print('Another instance of app is already running. Exiting.')
if not app.isRunning():
# Run the new instance if the app is not running
window = MainWindow() # Main window of the app
app.setApplicationName("PyMedia Player") # give name of the application
app.messageAvailable.connect(window.accept_songs) # Connect the message passing function for IPC
sys.exit(app.exec_()) # Run the app
# MainWindow is Shown from within the MainWindow class declaration
if __name__ == "__main__":
main()