基于Python實(shí)現(xiàn)本地音樂播放器的制作
制作這個(gè)播放器的目的是為了將下載下來的mp3文件進(jìn)行隨機(jī)或是順序的播放。選擇需要播放的音樂的路徑,選擇播放方式,經(jīng)過測試可以完美的播放本地音樂。
在開始之前介紹一個(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)過測試可以完美的播放本地音樂。
# 在開始之前介紹一個(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ù)字圖像處理之估計(jì)噪聲參數(shù)
這篇文章主要介紹了python數(shù)字圖像處理之估計(jì)噪聲參數(shù),圖像復(fù)原與重建,想了解圖像處理的同學(xué),一定要好好看看2021-04-04
python中Matplotlib實(shí)現(xiàn)繪制3D圖的示例代碼
本篇文章主要介紹了python中Matplotlib實(shí)現(xiàn)繪制3D圖的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Iconfont(矢量圖標(biāo))+iconmoon(圖標(biāo)svg互轉(zhuǎn))配合javascript實(shí)現(xiàn)社交分享系統(tǒng)
這篇文章主要介紹了Iconfont(矢量圖標(biāo))+iconmoon(圖標(biāo)svg互轉(zhuǎn))配合javascript實(shí)現(xiàn)社交分享系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python實(shí)現(xiàn)構(gòu)建一個(gè)儀表板的示例代碼
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)構(gòu)建一個(gè)儀表板,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-03-03
python實(shí)現(xiàn)簡單的購物程序代碼實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)簡單的購物程序代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

