-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
55 lines (44 loc) · 1.76 KB
/
sample.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from pydub import AudioSegment
from pydub.playback import play
class LosslessMusicPlayer(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Lossless Music Player")
self.setGeometry(100, 100, 400, 200)
self.player = None
self.play_button = QPushButton("Play")
self.play_button.clicked.connect(self.play_music)
self.open_button = QPushButton("Open Music File")
self.open_button.clicked.connect(self.open_music_file)
layout = QVBoxLayout()
layout.addWidget(self.play_button)
layout.addWidget(self.open_button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def open_music_file(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_name, _ = QFileDialog.getOpenFileName(self, "Open Music File", "", "Audio Files (*.flac *.mp3);;All Files (*)", options=options)
if file_name:
self.load_music(file_name)
def load_music(self, file_name):
if self.player:
self.player.stop()
audio = AudioSegment.from_file(file_name)
self.player = play(audio)
def play_music(self):
if self.player and self.player.is_playing():
self.player.stop()
self.play_button.setText("Play")
elif self.player:
self.player.resume()
self.play_button.setText("Pause")
if __name__ == "__main__":
app = QApplication(sys.argv)
player = LosslessMusicPlayer()
player.show()
sys.exit(app.exec_())