在这篇博客中,我们将使用 Python 的 PyQt5 和 pygame 库来创建一个简单的音乐播放器应用程序。我们的音乐播放器将具有基本的播放、暂停、上一首和下一首功能。
导入所需的库
首先,我们需要导入我们将在应用程序中使用的库:
import os
import pygame
import qtawesome as qta
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPixmap, QIcon, QBrush
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QMessageBox, QHBoxLayout, QVBoxLayout
创建 MusicPlayer 类
接下来,我们创建一个名为 MusicPlayer
的类,它继承自 QMainWindow
:
class MusicPlayer(QMainWindow):
def __init__(self, music_folder):
super().__init__()
self.setWindowTitle("LIY若依播放器")
self.setGeometry(100, 100, 2500, 1300)
self.setMinimumSize(2500, 1300)
self.setFixedSize(2500, 1300)
self.set_background("Anime-transformed.png")
pygame.mixer.init()
self.music_folder = music_folder
self.playlist = self.load_playlist()
self.current_track_index = 0
self.create_widgets()
在 __init__
方法中,我们初始化了一些基本的属性,如窗口标题、窗口大小、背景图片等。我们还初始化了 pygame 的音乐混音器,并加载了播放列表。
设置背景图片
我们使用 set_background
方法来设置窗口的背景图片:
def set_background(self, image_path):
self.setAutoFillBackground(True)
palette = self.palette()
pixmap = QPixmap(image_path).scaled(self.size(), Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
brush = QBrush(pixmap)
palette.setBrush(self.backgroundRole(), brush)
self.setPalette(palette)
加载播放列表
我们使用 load_playlist
方法来加载播放列表:
def load_playlist(self):
playlist = [os.path.join(self.music_folder, f) for f in os.listdir(self.music_folder) if f.endswith('.mp3')]
if not playlist:
error_dialog = QMessageBox(self)
error_dialog.setIcon(QMessageBox.Critical)
error_dialog.setText("在指定目录中未找到MP3文件")
error_dialog.setWindowTitle("错误")
error_dialog.exec_()
self.close()
return playlist
创建 UI 元素
我们使用 create_widgets
方法来创建 UI 元素:
def create_widgets(self):
main_layout = QVBoxLayout()
widget = QWidget(self)
widget.setLayout(main_layout)
self.setCentralWidget(widget)
button_layout = QHBoxLayout()
self.prev_button = QPushButton()
self.prev_button.setIcon(QIcon(qta.icon('fa.backward')))
self.prev_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.prev_button.setStyleSheet("background: transparent; border: none;")
self.prev_button.clicked.connect(self.prev_track)
button_layout.addWidget(self.prev_button, alignment=Qt.AlignCenter)
self.play_pause_button = QPushButton()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.play')))
self.play_pause_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.play_pause_button.setStyleSheet("background: transparent; border: none;")
self.play_pause_button.clicked.connect(self.play_pause)
button_layout.addWidget(self.play_pause_button, alignment=Qt.AlignCenter)
self.next_button = QPushButton()
self.next_button.setIcon(QIcon(qta.icon('fa.forward')))
self.next_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.next_button.setStyleSheet("background: transparent; border: none;")
self.next_button.clicked.connect(self.next_track)
button_layout.addWidget(self.next_button, alignment=Qt.AlignCenter)
main_layout.addStretch(1)
main_layout.addLayout(button_layout)
播放和暂停音乐
我们使用 play_pause
方法来播放和暂停音乐:
def play_pause(self):
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.play')))
else:
if not pygame.mixer.music.get_pos() > 0:
self.play_track()
else:
pygame.mixer.music.unpause()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.pause')))
播放音乐
我们使用 play_track
方法来播放音乐:
def play_track(self):
if self.playlist:
pygame.mixer.music.load(self.playlist[self.current_track_index])
pygame.mixer.music.play()
self.setWindowTitle(f"正在播放: {os.path.basename(self.playlist[self.current_track_index])}")
播放上一首和下一首音乐
我们使用 prev_track
和 next_track
方法来播放上一首和下一首音乐:
def prev_track(self):
self.current_track_index = (self.current_track_index - 1) % len(self.playlist)
self.play_track()
def next_track(self):
self.current_track_index = (self.current_track_index + 1) % len(self.playlist)
self.play_track()
运行应用程序
最后,我们在主程序中创建 MusicPlayer
的实例,并运行应用程序:
if __name__ == "__main__":
music_folder = r"D:\kugou"
app = QApplication([])
player = MusicPlayer(music_folder)
player.show()
app.exec_()
效果如下:
完整代码如下:
import os
import pygame
import qtawesome as qta
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPixmap, QIcon, QBrush
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QMessageBox, QHBoxLayout, QVBoxLayout
class MusicPlayer(QMainWindow):
def __init__(self, music_folder):
super().__init__()
self.setWindowTitle("LIY若依播放器")
self.setGeometry(100, 100, 2500, 1300)
self.setMinimumSize(2500, 1300)
self.setFixedSize(2500, 1300)
self.set_background("Anime-transformed.png")
pygame.mixer.init()
self.music_folder = music_folder
self.playlist = self.load_playlist()
self.current_track_index = 0
self.create_widgets()
def set_background(self, image_path):
self.setAutoFillBackground(True)
palette = self.palette()
pixmap = QPixmap(image_path).scaled(self.size(), Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
brush = QBrush(pixmap)
palette.setBrush(self.backgroundRole(), brush)
self.setPalette(palette)
def load_playlist(self):
playlist = [os.path.join(self.music_folder, f) for f in os.listdir(self.music_folder) if f.endswith('.mp3')]
if not playlist:
error_dialog = QMessageBox(self)
error_dialog.setIcon(QMessageBox.Critical)
error_dialog.setText("在指定目录中未找到MP3文件")
error_dialog.setWindowTitle("错误")
error_dialog.exec_()
self.close()
return playlist
def create_widgets(self):
main_layout = QVBoxLayout()
widget = QWidget(self)
widget.setLayout(main_layout)
self.setCentralWidget(widget)
button_layout = QHBoxLayout()
self.prev_button = QPushButton()
self.prev_button.setIcon(QIcon(qta.icon('fa.backward', options=[{'color': 'white'}])))
self.prev_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.prev_button.setStyleSheet("background: transparent; border: none;")
self.prev_button.clicked.connect(self.prev_track)
button_layout.addWidget(self.prev_button, alignment=Qt.AlignCenter)
self.play_pause_button = QPushButton()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.play', options=[{'color': 'white'}])))
self.play_pause_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.play_pause_button.setStyleSheet("background: transparent; border: none;")
self.play_pause_button.clicked.connect(self.play_pause)
button_layout.addWidget(self.play_pause_button, alignment=Qt.AlignCenter)
self.next_button = QPushButton()
self.next_button.setIcon(QIcon(qta.icon('fa.forward', options=[{'color': 'white'}])))
self.next_button.setIconSize(QSize(64, 64)) # 设置图标大小
self.next_button.setStyleSheet("background: transparent; border: none;")
self.next_button.clicked.connect(self.next_track)
button_layout.addWidget(self.next_button, alignment=Qt.AlignCenter)
main_layout.addStretch(1)
main_layout.addLayout(button_layout)
def play_pause(self):
if pygame.mixer.music.get_busy():
pygame.mixer.music.pause()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.play', options=[{'color': 'white'}])))
else:
if not pygame.mixer.music.get_pos() > 0:
self.play_track()
else:
pygame.mixer.music.unpause()
self.play_pause_button.setIcon(QIcon(qta.icon('fa.pause', options=[{'color': 'white'}])))
def play_track(self):
if self.playlist:
pygame.mixer.music.load(self.playlist[self.current_track_index])
pygame.mixer.music.play()
self.setWindowTitle(f"正在播放: {os.path.basename(self.playlist[self.current_track_index])}")
def prev_track(self):
self.current_track_index = (self.current_track_index - 1) % len(self.playlist)
self.play_track()
def next_track(self):
self.current_track_index = (self.current_track_index + 1) % len(self.playlist)
self.play_track()
if __name__ == "__main__":
music_folder = r"D:\kugou"
app = QApplication([])
player = MusicPlayer(music_folder)
player.show()
app.exec_()
这就是我的音乐播放器应用程序的全部代码。希望你喜欢这篇博客,并从中学到了一些有用的知识。如果你有任何问题或者建议,欢迎在评论区留言。
标签:播放器,play,pause,python,self,track,music,100,button From: https://blog.csdn.net/m0_74972192/article/details/140357128