基于Python實(shí)現(xiàn)本地音樂播放器的制作
制作這個(gè)播放器的目的是為了將下載下來的mp3文件進(jìn)行隨機(jī)或是順序的播放。選擇需要播放的音樂的路徑,選擇播放方式,經(jīng)過測(cè)試可以完美的播放本地音樂。
在開始之前介紹一個(gè)免費(fèi)下載mp3音樂的網(wǎng)站,有需要的可以下載自己喜歡的音樂。當(dāng)然有各大音樂平臺(tái)會(huì)員的大佬就不需要了。
缺少音樂素材的可以去免費(fèi)下載即可,準(zhǔn)備好音樂素材后將其放到一個(gè)文件夾下面即可。
在應(yīng)用實(shí)現(xiàn)過程中,總共使用了下面這些庫,特別需要注意的是這個(gè)庫playsound使用的版本是1.3.0,聽說其他版本在播放音樂時(shí)可能存在問題。也可以將播放音樂的部分換成其他的實(shí)現(xiàn)方式。
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys from QCandyUi import CandyWindow import random, os from playsound import playsound
最先實(shí)現(xiàn)的是播放音樂的業(yè)務(wù)邏輯,這里是采用pyqt5自帶的QThread線程來實(shí)現(xiàn)的,目的是將播放音樂的部分作為一個(gè)子線程來運(yùn)行,防止與UI界面的主線程產(chǎn)生阻塞。
實(shí)現(xiàn)子線程的部分是一樣的范式,一般情況下按照這種范式實(shí)現(xiàn),屢試不爽。在前面的UI桌面應(yīng)用中幾乎都是使用這種方式來實(shí)現(xiàn)多線程的。
class PlayThread(QThread): finished = pyqtSignal(bool) def __init__(self, parent=None): super(PlayThread, self).__init__(parent) self.parent = parent self.working = True def __del__(self): self.working = False self.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() == '隨機(jī)播放': 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)
音樂播放的業(yè)務(wù)邏輯實(shí)現(xiàn)完成了,接下來來實(shí)現(xiàn)UI界面的部分。應(yīng)用就是簡單的設(shè)計(jì)了一下不是很復(fù)雜。
pyqt5的UI界面的實(shí)現(xiàn)方式主要是組件的布局和槽函數(shù)的引用,下面是UI界面布局及各個(gè)槽函數(shù)的初始化及引用。以及如何界面的主線程中調(diào)用子線程的使用。
class MusicUI(QWidget): def __init__(self): super(MusicUI, self).__init__() self.init_ui() def init_ui(self): self.setWindowTitle('本地音樂播放器 公眾號(hào):[Python 集中營]') 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('隨機(jī)播放') 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函數(shù)將界面布局的整個(gè)過程加入到主體循環(huán)中就大功告成了。 if __name__ == '__main__': app = QApplication(sys.argv) w = CandyWindow.createWindow(MusicUI(), theme='blue', title='本地音樂播放器 公眾號(hào):[Python 集中營]', ico_path='音樂.ico') w.show() sys.exit(app.exec_())
完整代碼
# -*- coding:utf-8 -*- # @author Python 集中營 # @date 2022/4/23 # @file test10.py # done # python 本地音樂播放器制作過程(附完整源碼) # 文摘:通過pyqt5多線程制作簡單的本地音樂播放器... # 制作這個(gè)播放器的目的是為了將下載下來的mp3文件進(jìn)行隨機(jī)或是順序的播放。選擇需要播放的音樂的路徑,選擇播放方式, # 經(jīng)過測(cè)試可以完美的播放本地音樂。 # 在開始之前介紹一個(gè)免費(fèi)下載mp3音樂的網(wǎng)站,有需要的可以下載自己喜歡的音樂。當(dāng)然有各大音樂平臺(tái)會(huì)員的大佬就不需要了。 # http://music.y444.cn/#/ # 缺少音樂素材的可以去免費(fèi)下載即可,準(zhǔn)備好音樂素材后將其放到一個(gè)文件夾下面即可。 # 在應(yīng)用實(shí)現(xiàn)過程中,總共使用了下面這些庫,特別需要注意的是這個(gè)庫playsound使用的版本是1.3.0,聽說其他版本在播放音樂時(shí)可能存在問題。 # 也可以將播放音樂的部分換成其他的實(shí)現(xiàn)方式。 from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys from QCandyUi import CandyWindow import random, os from playsound import playsound # 最先實(shí)現(xiàn)的是播放音樂的業(yè)務(wù)邏輯,這里是采用pyqt5自帶的QThread線程來實(shí)現(xiàn)的,目的是將播放音樂的部分 # 作為一個(gè)子線程來運(yùn)行,防止與UI界面的主線程產(chǎn)生阻塞。 # 實(shí)現(xiàn)子線程的部分是一樣的范式,一般情況下按照這種范式實(shí)現(xiàn),屢試不爽。在前面的UI桌面應(yīng)用中幾乎都是使用這種方式來實(shí)現(xiàn)多線程的。 class PlayThread(QThread): finished = pyqtSignal(bool) def __init__(self, parent=None): super(PlayThread, self).__init__(parent) self.parent = parent self.working = True def __del__(self): self.working = False self.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() == '隨機(jī)播放': 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) # 音樂播放的業(yè)務(wù)邏輯實(shí)現(xiàn)完成了,接下來來實(shí)現(xiàn)UI界面的部分。應(yīng)用就是簡單的設(shè)計(jì)了一下不是很復(fù)雜。 # 音樂播放器UI.png # pyqt5的UI界面的實(shí)現(xiàn)方式主要是組件的布局和槽函數(shù)的引用,下面是UI界面布局及各個(gè)槽函數(shù)的初始化及引用。 # 以及如何界面的主線程中調(diào)用子線程的使用。 class MusicUI(QWidget): def __init__(self): super(MusicUI, self).__init__() self.init_ui() def init_ui(self): self.setWindowTitle('本地音樂播放器 公眾號(hào):[Python 集中營]') 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('隨機(jī)播放') 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函數(shù)將界面布局的整個(gè)過程加入到主體循環(huán)中就大功告成了。 if __name__ == '__main__': app = QApplication(sys.argv) w = CandyWindow.createWindow(MusicUI(), theme='blue', title='本地音樂播放器 公眾號(hào):[Python 集中營]', ico_path='音樂.ico') w.show() sys.exit(app.exec_())
以上就是基于Python實(shí)現(xiàn)本地音樂播放器的制作的詳細(xì)內(nèi)容,更多關(guān)于Python本地音樂播放器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)時(shí)間序列可視化的方法
matplotlib庫是一個(gè)用于創(chuàng)建出版質(zhì)量圖表的桌面繪圖包(2D繪圖庫),是Python中最基本的可視化工具。這篇文章主要介紹了Python時(shí)間序列可視化實(shí)現(xiàn),需要的朋友可以參考下2019-08-08python中常用檢測(cè)字符串相關(guān)函數(shù)匯總
這篇文章主要介紹了python中常用檢測(cè)字符串相關(guān)函數(shù),實(shí)例匯總了Python針對(duì)字符串?dāng)?shù)字、字母、大小寫等常用檢測(cè)函數(shù),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04python基礎(chǔ)入門之普通操作與函數(shù)(三)
這篇文章主要介紹了python基礎(chǔ)入門之普通操作與函數(shù)2021-06-06Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例
HDFS是Apache Hadoop的分布式文件系統(tǒng),本文主要介紹了Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Python畫圖工具M(jìn)atplotlib庫常用命令簡述
這篇文章主要介紹了Python畫圖Matplotlib庫常用命令簡述總結(jié),文中包含詳細(xì)的圖文示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09django foreignkey(外鍵)的實(shí)現(xiàn)
這篇文章主要介紹了django foreignkey(外鍵)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07