第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 基于Python实现本地音乐播放器的制作

基于Python实现本地音乐播放器的制作

时间:2023-08-15 00:03:48

相关推荐

基于Python实现本地音乐播放器的制作

制作这个播放器的目的是为了将下载下来的mp3文件进行随机或是顺序的播放。选择需要播放的音乐的路径,选择播放方式,经过测试可以完美的播放本地音乐。

在开始之前介绍一个免费下载mp3音乐的网站,有需要的可以下载自己喜欢的音乐。当然有各大音乐平台会员的大佬就不需要了。

缺少音乐素材的可以去免费下载即可,准备好音乐素材后将其放到一个文件夹下面即可。

在应用实现过程中,总共使用了下面这些库,特别需要注意的是这个库playsound使用的版本是1.3.0,听说其他版本在播放音乐时可能存在问题。也可以将播放音乐的部分换成其他的实现方式。

from PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.QtGui import *import sysfrom QCandyUi import CandyWindowimport random, osfrom playsound import playsound

最先实现的是播放音乐的业务逻辑,这里是采用pyqt5自带的QThread线程来实现的,目的是将播放音乐的部分作为一个子线程来运行,防止与UI界面的主线程产生阻塞。

实现子线程的部分是一样的范式,一般情况下按照这种范式实现,屡试不爽。在前面的UI桌面应用中几乎都是使用这种方式来实现多线程的。

class PlayThread(QThread):finished = pyqtSignal(bool)def __init__(self, parent=None):super(PlayThread, self).__init__(parent)self.parent = parentself.working = Truedef __del__(self):self.working = Falseself.wait()def run(self):music_files = os.listdir(self.parent.music_file_path.text())print(music_files)for index in range(0, len(music_files) - 1):if self.parent.play_type_selected.currentText() == '随机播放':index = random.randint(0, len(music_files) - 1)print(index)playsound(os.path.join(self.parent.music_file_path.text(), music_files[index]))self.finished.emit(True)

音乐播放的业务逻辑实现完成了,接下来来实现UI界面的部分。应用就是简单的设计了一下不是很复杂。

pyqt5的UI界面的实现方式主要是组件的布局和槽函数的引用,下面是UI界面布局及各个槽函数的初始化及引用。以及如何界面的主线程中调用子线程的使用。

class MusicUI(QWidget):def __init__(self):super(MusicUI, self).__init__()self.init_ui()def init_ui(self):self.setWindowTitle('本地音乐播放器 [Python学习群:780296133 ]')self.setWindowIcon(QIcon('音乐.ico'))self.setFixedWidth(500)self.setFixedHeight(100)hbox1 = QHBoxLayout()self.music_file_path = QLineEdit()self.music_file_path.setReadOnly(True)self.music_file_btn = QPushButton()self.music_file_btn.setText('路径')self.music_file_btn.clicked.connect(self.music_file_btn_click)hbox1.addWidget(self.music_file_path)hbox1.addWidget(self.music_file_btn)hbox2 = QHBoxLayout()self.play_type_selected = QComboBox()self.play_type_selected.addItem('随机播放')self.play_type_selected.addItem('顺序播放')self.start_btn = QPushButton()self.start_btn.setText('开始播放')self.start_btn.clicked.connect(self.start_btn_click)hbox2.addWidget(self.play_type_selected)hbox2.addWidget(self.start_btn)vbox = QVBoxLayout()vbox.addLayout(hbox1)vbox.addLayout(hbox2)self.thread_ = PlayThread(self)self.thread_.finished.connect(self.finished)self.setLayout(vbox)def music_file_btn_click(self):dir = QFileDialog.getExistingDirectory(self, "选择文件夹", os.getcwd())self.music_file_path.setText(dir)def start_btn_click(self):self.start_btn.setEnabled(False)self.thread_.start()def finished(self,finished):if finished is True:self.start_btn.setEnabled(True)# 最后,使用mian函数将界面布局的整个过程加入到主体循环中就大功告成了。if __name__ == '__main__':app = QApplication(sys.argv)w = CandyWindow.createWindow(MusicUI(), theme='blue', title='本地音乐播放器 Python学习群:780296133',ico_path='音乐.ico')w.show()sys.exit(app.exec_())

完整代码

# -*- coding:utf-8 -*-# @author Python学习交流群:780296133# @date /1/08# @file test10.py# done# python 本地音乐播放器制作过程(附完整源码)# 文摘:通过pyqt5多线程制作简单的本地音乐播放器...# 制作这个播放器的目的是为了将下载下来的mp3文件进行随机或是顺序的播放。选择需要播放的音乐的路径,选择播放方式,# 经过测试可以完美的播放本地音乐。# 在开始之前介绍一个免费下载mp3音乐的网站,有需要的可以下载自己喜欢的音乐。当然有各大音乐平台会员的大佬就不需要了。# /#/# 缺少音乐素材的可以去免费下载即可,准备好音乐素材后将其放到一个文件夹下面即可。# 在应用实现过程中,总共使用了下面这些库,特别需要注意的是这个库playsound使用的版本是1.3.0,听说其他版本在播放音乐时可能存在问题。# 也可以将播放音乐的部分换成其他的实现方式。from PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.QtGui import *import sysfrom QCandyUi import CandyWindowimport random, osfrom playsound import playsound# 最先实现的是播放音乐的业务逻辑,这里是采用pyqt5自带的QThread线程来实现的,目的是将播放音乐的部分# 作为一个子线程来运行,防止与UI界面的主线程产生阻塞。# 实现子线程的部分是一样的范式,一般情况下按照这种范式实现,屡试不爽。在前面的UI桌面应用中几乎都是使用这种方式来实现多线程的。class PlayThread(QThread):finished = pyqtSignal(bool)def __init__(self, parent=None):super(PlayThread, self).__init__(parent)self.parent = parentself.working = Truedef __del__(self):self.working = Falseself.wait()def run(self):music_files = os.listdir(self.parent.music_file_path.text())print(music_files)for index in range(0, len(music_files) - 1):if self.parent.play_type_selected.currentText() == '随机播放':index = random.randint(0, len(music_files) - 1)print(index)playsound(os.path.join(self.parent.music_file_path.text(), music_files[index]))self.finished.emit(True)# 音乐播放的业务逻辑实现完成了,接下来来实现UI界面的部分。应用就是简单的设计了一下不是很复杂。# 音乐播放器UI.png# pyqt5的UI界面的实现方式主要是组件的布局和槽函数的引用,下面是UI界面布局及各个槽函数的初始化及引用。# 以及如何界面的主线程中调用子线程的使用。class MusicUI(QWidget):def __init__(self):super(MusicUI, self).__init__()self.init_ui()def init_ui(self):self.setWindowTitle('本地音乐播放器 Python学习交流群:780296133')self.setWindowIcon(QIcon('音乐.ico'))self.setFixedWidth(500)self.setFixedHeight(100)hbox1 = QHBoxLayout()self.music_file_path = QLineEdit()self.music_file_path.setReadOnly(True)self.music_file_btn = QPushButton()self.music_file_btn.setText('路径')self.music_file_btn.clicked.connect(self.music_file_btn_click)hbox1.addWidget(self.music_file_path)hbox1.addWidget(self.music_file_btn)hbox2 = QHBoxLayout()self.play_type_selected = QComboBox()self.play_type_selected.addItem('随机播放')self.play_type_selected.addItem('顺序播放')self.start_btn = QPushButton()self.start_btn.setText('开始播放')self.start_btn.clicked.connect(self.start_btn_click)hbox2.addWidget(self.play_type_selected)hbox2.addWidget(self.start_btn)vbox = QVBoxLayout()vbox.addLayout(hbox1)vbox.addLayout(hbox2)self.thread_ = PlayThread(self)self.thread_.finished.connect(self.finished)self.setLayout(vbox)def music_file_btn_click(self):dir = QFileDialog.getExistingDirectory(self, "选择文件夹", os.getcwd())self.music_file_path.setText(dir)def start_btn_click(self):self.start_btn.setEnabled(False)self.thread_.start()def finished(self,finished):if finished is True:self.start_btn.setEnabled(True)# 最后,使用mian函数将界面布局的整个过程加入到主体循环中就大功告成了。if __name__ == '__main__':app = QApplication(sys.argv)w = CandyWindow.createWindow(MusicUI(), theme='blue', title='本地音乐播放器 Python学习交流群:780296133',ico_path='音乐.ico')w.show()sys.exit(app.exec_())

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。