-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.py
142 lines (121 loc) · 4.1 KB
/
functional.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
from PyQt6.QtCore import QThread, QTimer
from PyQt6.QtCore import QFile, QTextStream
from PyQt6.QtGui import QIcon
import os
import pygame
class MusicPlayer(QThread):
playlist = []
current = 0
paused = True
isstop = True
duration = 0
pos = 0
def __init__(self, parent):
super(MusicPlayer, self).__init__()
pygame.init()
self.parent = parent
timer = QTimer()
timer.timeout.connect(self.update_slider)
self.timer = timer
pygame.mixer.init()
def update_slider(self):
try:
slider = self.parent.PlayerFrame.MusicSlider
slider.setValue(self.pos)
self.pos += 1
self.parent.PlayerFrame.Labels.set_text(self.format_time(self.pos), self.format_time(self.duration - self.pos))
if self.pos >= slider.maximum():
self.next()
except Exception as ex:
print(ex)
def set_slider(self):
slider = self.parent.PlayerFrame.MusicSlider
sound = pygame.mixer.Sound(self.playlist[self.current])
duration = sound.get_length()
self.duration = duration
slider.setMaximum(int(duration))
def run(self):
...
def get_playlist(self, path):
try:
self.isstop = False
extensions = [".mp3", ".wav", ".wma"]
file_paths = []
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isfile(file_path) and any(file.endswith(extension) for extension in extensions):
file_paths.append(file_path)
self.playlist = file_paths
self.current = -1
self.pos = 0
self.next()
except Exception as e:
print(e)
def play_stop(self):
if not self.isstop:
if self.paused:
self.paused = False
pygame.mixer.music.unpause()
self.timer.start(1000)
else:
self.paused = True
pygame.mixer.music.pause()
self.timer.stop()
def next(self):
try:
self.pos = 0
self.current += 1
if self.current >= len(self.playlist):
self.current = 0
pygame.mixer.music.load(self.playlist[self.current])
pygame.mixer.music.play()
if self.paused:
pygame.mixer.music.pause()
self.pos = 0
self.set_slider()
self.update_slider()
except Exception as ex:
print(ex)
def prev(self):
try:
self.pos = 0
self.current -= 1
if self.current < 0:
self.current = len(self.playlist)
pygame.mixer.music.load(self.playlist[self.current])
pygame.mixer.music.play()
if self.paused:
pygame.mixer.music.pause()
self.pos = 0
self.set_slider()
self.update_slider()
except Exception as ex:
print(ex)
def set_pos(self, pos):
pygame.mixer.music.set_pos(pos)
self.pos = pos
def set_volume(self, volume):
volume = volume / 100
pygame.mixer.music.set_volume(volume)
def format_time(self, seconds):
minutes = int(seconds // 60)
seconds = int(seconds % 60)
return f"{minutes}:{seconds:02d}"
def stop(self):
pygame.mixer.music.stop()
self.playlist = []
self.paused = True
self.isstop = True
self.timer.stop()
self.parent.PlayerFrame.Labels.set_text(self.format_time(0), self.format_time(0))
self.parent.PlayerFrame.PlayButton.setIcon(QIcon("style/resources/play.svg"))
self.parent.PlayerFrame.MusicSlider.setValue(0)
class StyleSheet(QFile):
def __init__(self, stylesheet):
path = "style/" + stylesheet
super().__init__(path)
def __enter__(self):
self.open(QFile.OpenModeFlag.ReadOnly | QFile.OpenModeFlag.Text)
return QTextStream(self).readAll()
def __exit__(self, *args):
self.close()