用Python寫了一款炫酷音樂播放器,想聽啥隨便搜!
本文鏈接:
https://blog.csdn.net/hhladminhhl/article/details/118915410
前言
晚上坐在電腦面前,想著一邊擼代碼,一邊聽音樂。搜了搜自己想聽的歌,奈何好多歌曲都提示需要版權(quán),無法播放!
沒辦法,想聽歌還是得靠自己解決!今天就一起用python自制一款炫酷的音樂播放器吧~
首先一起來看看最終實現(xiàn)的音樂播放器效果:

下面,我們開始介紹這個音樂播放器的制作過程。
一、核心功能設(shè)計
總體來說,我們首先需要設(shè)計UI界面,對播放器的畫面布局進(jìn)行排版設(shè)計;其次我們的這款音樂播放器的主要功能包括根據(jù)關(guān)鍵字搜索自動爬取音樂,獲取音樂列表,能進(jìn)行音樂播放。
當(dāng)然還少不了一些附加功能,例如播放方式列表循環(huán)、單曲循環(huán)、隨機(jī)播放,當(dāng)前上一首下一首播放,播放暫停開始,音量增加減少,播放歷史查看等。
拆解需求,大致可以整理出核心功能如下:
UI設(shè)計排版布局
頭部主要包括關(guān)鍵字搜索和音樂來源選擇,以及窗體最小化,最大化,關(guān)閉功能
中間主體包含左右兩側(cè),左側(cè)用來顯示播放音樂封面圖,右側(cè)用來進(jìn)行音樂列表顯示
底部主要來顯示當(dāng)前播放音樂,播放進(jìn)度條,音量控制,上一首/下一首,暫停/開始,播放方式等附加功能
關(guān)鍵字音樂列表爬蟲
通過輸入的搜索關(guān)鍵字和選擇的音樂來源,自動爬取對應(yīng)的音樂數(shù)據(jù)
將爬取獲取的音樂名進(jìn)行列表顯示,顯示在中間主體搜索頁
音樂播放
音樂列表中我們需要雙擊某一首歌,對爬取的歌曲封面圖和歌曲進(jìn)行下載
下載成功,對音樂文件根據(jù)播放進(jìn)度條進(jìn)行播放
附加功能
播放音樂時,我們還需要有播放暫停和啟動功能
音量控制提高或者降低
當(dāng)前播放歌曲上一首、下一首
音樂列表播放方式,列表循環(huán)、單曲循環(huán)、隨機(jī)播放
二、實現(xiàn)步驟
1. UI設(shè)計排版布局
基于功能點,我們首先考慮進(jìn)行簡單的UI布局設(shè)計,這里我們使用的是pyqt5。核心設(shè)計代碼如下:
def init_ui(self):global typeself.setFixedSize(1025, 750)self.main_widget = QWidget() # 創(chuàng)建窗口主部件self.main_layout = QGridLayout() # 創(chuàng)建主部件的網(wǎng)格布局self.main_widget.setLayout(self.main_layout) # 設(shè)置窗口主部件布局為網(wǎng)格布局self.close_widget = QWidget() # 創(chuàng)建關(guān)閉側(cè)部件self.close_widget.setObjectName('close_widget')self.close_layout = QGridLayout() # 創(chuàng)建左側(cè)部件的網(wǎng)格布局層self.close_widget.setLayout(self.close_layout) # 設(shè)置左側(cè)部件布局為網(wǎng)格self.left_widget = QWidget() # 創(chuàng)建左邊側(cè)部件self.left_widget.setObjectName('left_widget')self.left_layout = QGridLayout() # 創(chuàng)建左側(cè)部件的網(wǎng)格布局層self.left_widget.setLayout(self.left_layout) # 設(shè)置左側(cè)部件布局為網(wǎng)格self.right_widget = QWidget() # 創(chuàng)建右側(cè)部件self.right_widget.setObjectName('right_widget')self.right_layout = QGridLayout()self.right_widget.setLayout(self.right_layout) # 設(shè)置右側(cè)部件布局為網(wǎng)格self.down_widget = QWidget() # 創(chuàng)建下面部件self.down_widget.setObjectName('down_widget')self.down_layout = QGridLayout()self.down_widget.setLayout(self.down_layout) # 設(shè)置下側(cè)部件布局為網(wǎng)格self.up_widget = QWidget() # 創(chuàng)建下面部件self.up_widget.setObjectName('up_widget')self.up_layout = QGridLayout()self.up_widget.setLayout(self.up_layout) # 設(shè)置下側(cè)部件布局為網(wǎng)格self.label = QLabel(self)self.label.setText("還沒有播放歌曲呢╰(*°▽°*)╯")self.label.setStyleSheet("color:white")self.label.setMaximumSize(310, 20)self.main_layout.addWidget(self.up_widget, 0, 0, 1, 115)self.main_layout.addWidget(self.left_widget, 1, 0, 90, 25)self.main_layout.addWidget(self.right_widget, 1, 25, 90, 90) # 22右側(cè)部件在第0行第3列,占8行9列self.main_layout.addWidget(self.down_widget, 100, 0, 10, 115)self.main_layout.addWidget(self.close_widget, 0, 110, 1, 5) # 左側(cè)部件在第0行第0列,占1行3列self.down_layout.addWidget(self.label, 1, 0, 1, 1)self.setCentralWidget(self.main_widget) # 設(shè)置窗口主部件self.tabWidget = QTabWidget(self)self.tabWidget.setGeometry(QRect(33, 20, 716, 471))self.tab = QWidget()self.tab.setObjectName("tab")self.tab_layout = QGridLayout()self.tab.setLayout(self.tab_layout)self.listwidget = QListWidget(self.tab)self.listwidget.doubleClicked.connect(lambda: self.change_func(self.listwidget))self.listwidget.setContextMenuPolicy(Qt.CustomContextMenu)self.listwidget.customContextMenuRequested[QPoint].connect(self.myListWidgetContext)self.listwidget.setObjectName("listWidget")self.tab_layout.addWidget(self.listwidget, 0, 0, 1, 1)self.tabWidget.addTab(self.tab, " 搜索頁 ")self.tab2 = QWidget()self.tab2.setObjectName("tab")self.tab2_layout = QGridLayout()self.tab2.setLayout(self.tab2_layout)self.listwidget2 = QListWidget(self.tab2)self.listwidget2.doubleClicked.connect(lambda: self.change_funcse(self.listwidget2))self.listwidget2.setContextMenuPolicy(Qt.CustomContextMenu)self.listwidget2.customContextMenuRequested[QPoint].connect(self.myListWidgetContext2)self.listwidget2.setObjectName("listWidget2")self.listwidget2.setContextMenuPolicy(3)self.tab2_layout.addWidget(self.listwidget2, 0, 0, 1, 1)self.tabWidget.addTab(self.tab2, " 最近播放 ")self.right_layout.addWidget(self.tabWidget, 3, 0, 100, 90)self.left_close = QPushButton("") # 關(guān)閉按鈕self.left_close.clicked.connect(self.close)self.left_visit = QPushButton("") # 空白按鈕self.left_visit.clicked.connect(self.big)self.left_mini = QPushButton("") # 最小化按鈕self.left_mini.clicked.connect(self.mini)self.close_layout.addWidget(self.left_mini, 0, 0, 1, 1)self.close_layout.addWidget(self.left_close, 0, 2, 1, 1)self.close_layout.addWidget(self.left_visit, 0, 1, 1, 1)self.left_close.setFixedSize(15, 15) # 設(shè)置關(guān)閉按鈕的大小self.left_visit.setFixedSize(15, 15) # 設(shè)置按鈕大小self.left_mini.setFixedSize(15, 15) # 設(shè)置最小化按鈕大小self.left_close.setStyleSheet('''QPushButton{background:#F76677;border-radius:5px;}QPushButton:hover{background:red;}''')self.left_visit.setStyleSheet('''QPushButton{background:#F7D674;border-radius:5px;}QPushButton:hover{background:yellow;}''')self.left_mini.setStyleSheet('''QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}''')self.button_123 = QLabel("")self.left_layout.addWidget(self.button_123, 0, 2, 2, 2)self.label2 = QLabel(self)self.label2.setText("當(dāng)前為順序播放")self.label2.setStyleSheet("color:green")self.left_layout.addWidget(self.label2, 4, 0, 2, 1)self.button_1234 = QPushButton(icon('fa.download', color='#3FC89C', font=24), "")self.button_1234.clicked.connect(self.down)self.button_1234.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.left_layout.addWidget(self.button_1234, 4, 2, 2, 1)self.button_1234 = QPushButton(icon('fa.heart', color='#3FC89C', font=24), "")self.button_1234.clicked.connect(self.lovesong)self.button_1234.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.left_layout.addWidget(self.button_1234, 4, 3, 2, 2)self.label3 = QLabel(self)self.label3.setText("")self.label3.setStyleSheet("color:white")self.down_layout.addWidget(self.label3, 1, 3, 1, 1)self.label7 = QLabel(self)self.label7.setText("")self.label7.setStyleSheet("color:white")self.label5 = QLabel(self)pix_img = QPixmap(str(data + '/backdown.png'))pix = pix_img.scaled(300, 300, Qt.KeepAspectRatio)self.label5.setPixmap(pix)# self.label5.setMaximumSize(1,1)self.left_layout.addWidget(self.label5, 2, 0, 2, 8)self.label6 = QLabel(self)self.label6.setText("")self.label6.setStyleSheet("color:#6DDF6D")self.left_layout.addWidget(self.label6, 2, 0, 2, 2)self.label23 = QLabel(self)self.label23.setText(" ")self.label23.setStyleSheet("color:#6DDF6D")self.up_layout.addWidget(self.label23, 0, 100, 1, 20)self.shuru = QLineEdit("")self.up_layout.addWidget(self.shuru, 0, 120, 1, 40)self.shuru.returnPressed.connect(self.correct)self.label23 = QLabel(self)self.label23.setText(" 軟件")self.label23.setStyleSheet("color:#6DDF6D")self.up_layout.addWidget(self.label23, 0, 160, 1, 10)self.label61 = QLabel(self)self.label61.setText("")self.label61.setStyleSheet("color:#6DDF6D")self.up_layout.addWidget(self.label61, 0, 200, 1, 50)self.cb = QComboBox(self)self.cb.addItems(['網(wǎng)易云', '酷狗', 'qq'])self.up_layout.addWidget(self.cb, 0, 180, 1, 30)self.cb.currentIndexChanged[int].connect(self.print)self.button_1 = QPushButton(icon('fa.search', color='white'), "")self.button_1.clicked.connect(self.correct)self.button_1.setStyleSheet('''QPushButton{color:white;border-radius:5px;}QPushButton:hover{background:green;}''')self.up_layout.addWidget(self.button_1, 0, 155, 1, 5)self.right_process_bar = QProgressBar() # 播放進(jìn)度部件self.right_process_bar.setValue(49)self.right_process_bar.setFixedHeight(3) # 設(shè)置進(jìn)度條高度self.right_process_bar.setTextVisible(False) # 不顯示進(jìn)度條文字self.right_process_bar.setRange(0, 10000)self.right_playconsole_widget = QWidget() # 播放控制部件self.right_playconsole_layout = QGridLayout() # 播放控制部件網(wǎng)格布局層self.right_playconsole_widget.setLayout(self.right_playconsole_layout)self.console_button_1 = QPushButton(icon('fa.backward', color='#3FC89C'), "")self.console_button_1.clicked.connect(self.last)self.console_button_1.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_2 = QPushButton(icon('fa.forward', color='#3FC89C'), "")self.console_button_2.clicked.connect(self.nextion)self.console_button_2.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_3 = QPushButton(icon('fa.pause', color='#3FC89C', font=18), "")self.console_button_3.clicked.connect(self.pause)self.console_button_3.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_4 = QPushButton(icon('fa.volume-down', color='#3FC89C', font=18), "")self.console_button_4.clicked.connect(self.voicedown)self.console_button_4.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_5 = QPushButton(icon('fa.volume-up', color='#3FC89C', font=18), "")self.console_button_5.clicked.connect(self.voiceup)self.console_button_5.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_6 = QPushButton(icon('fa.align-center', color='#3FC89C', font=18), "")self.console_button_6.clicked.connect(self.playmode)self.console_button_6.setStyleSheet('''QPushButton{background:#222225;border-radius:5px;}QPushButton:hover{background:#3684C8;}''')self.console_button_3.setIconSize(QSize(30, 30))self.right_playconsole_layout.addWidget(self.console_button_4, 0, 0)self.right_playconsole_layout.addWidget(self.console_button_1, 0, 1)self.right_playconsole_layout.addWidget(self.console_button_3, 0, 2)self.right_playconsole_layout.addWidget(self.console_button_2, 0, 3)self.right_playconsole_layout.addWidget(self.console_button_5, 0, 4)self.right_playconsole_layout.addWidget(self.console_button_6, 0, 5)self.right_playconsole_layout.setAlignment(Qt.AlignCenter) # 設(shè)置布局內(nèi)部件居中顯示self.down_layout.addWidget(self.right_process_bar, 0, 0, 1, 4) # 第0行第0列,占8行3列# 第0行第0列,占8行3列self.down_layout.addWidget(self.label7, 1, 2, 1, 1)self.down_layout.addWidget(self.right_playconsole_widget, 1, 0, 1, 4)self.setWindowOpacity(0.95) # 設(shè)置窗口透明度self.setAttribute(Qt.WA_TranslucentBackground)self.setWindowFlag(Qt.FramelessWindowHint) # 隱藏邊框????self.main_layout.setSpacing(0)
實現(xiàn)效果如下:
2. 關(guān)鍵字音樂列表爬蟲
我們可以根據(jù)輸入的關(guān)鍵字和音樂來源進(jìn)行音樂爬取。這里我們需要通過多線程,將歌曲、歌手、歌曲url地址全都獲取。核心代碼如下:
def run(self):qmut.lock()try:global paingglobal stopglobal lrcsglobal urlsglobal songsglobal nameglobal songidglobal proxiesglobal picglobal tryedpaing = Trueprint('搜索軟件{}'.format(type))print('開始搜索')name = nameheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.110.430.128 Safari/537.36','X-Requested-With': 'XMLHttpRequest'}urls = []songs = []pic = []lrcs = []pages = 5print(pages)for a in range(0, pages):if not stop:urlss = ['http://music.9q4.cn/', 'https://defcon.cn/dmusic/','http://www.xmsj.org/','http://music.laomao.me/']print (tryed)if tryed >3:tryed = 0url = urlss[tryed]else:url = urlss[tryed]print (urlss[tryed])params = {'input': name,'filter': 'name','type': type,'page': a}if not stop:try:res = post(url, params, headers=headers, proxies=proxies)html = res.json()for i in range(0, 10):try:title = jsonpath(html, '$..title')[i]author = jsonpath(html, '$..author')[i]url1 = jsonpath(html, '$..url')[i] # 取下載網(wǎng)址pick = jsonpath(html, '$..pic')[i] # 取歌詞lrc = jsonpath(html, '$..lrc')[i]print(title, author)lrcs.append(lrc)urls.append(url1)pic.append(pick)songs.append(str(title) + ' - ' + str(author))except:passexcept:stop = Falsepaing = Falseself.trigger.emit(str('finish'))else:self.trigger.emit(str('finish'))else:self.trigger.emit(str('clear'))passstop = Falsepaing = Falseexcept:print('爬取歌曲出錯')self.trigger.emit(str('unfinish'))stop = Falsepaing = False??qmut.unlock()
爬取代碼寫好了,我們還需要將爬取的這些音樂數(shù)據(jù)列表顯示到畫面中搜索頁面中。代碼如下:
def repite(self, name, type):global tryedglobal paingglobal stopself.listwidget.clear()self.listwidget.addItem('搜索中')self.listwidget.item(0).setForeground(Qt.white)try:if paing:stop = Trueself.listwidget.clear()self.work2 = PAThread()self.work2.start()self.work2.trigger.connect(self.seafinish)else:self.work2 = PAThread()self.work2.start()self.work2.trigger.connect(self.seafinish)except:tryed = tryed + 1get_info('https://www.kuaidaili.com/free/inha')self.listwidget.addItem('貌似沒網(wǎng)了呀`(*>﹏<*)′,再試一遍吧~')????????self.listwidget.item(0).setForeground(Qt.white)
效果如下:
3. 音樂播放
關(guān)鍵字搜索音樂列表完成之后,接下來我們需要實現(xiàn)音樂播放功能。首先需要從爬取的url下載待播放的歌曲,這里還是通過多線程進(jìn)行。核心代碼如下:
class WorkThread(QThread):trigger = pyqtSignal(str)def __int__(self):# 初始化函數(shù)super(WorkThread, self).__init__()# 進(jìn)度條def cbk(self, a, b, c):'''''回調(diào)函數(shù)@a:已經(jīng)下載的數(shù)據(jù)塊@b:數(shù)據(jù)塊的大小@c:遠(yuǎn)程文件的大小'''per = 100.0 * a * b / cif per > 100:per = 100# print ('%.2f%%' % per)self.trigger.emit(str('%.2f%%' % per))def run(self):try:global numberglobal pathglobal downloadingtry:proxies = {'http': 'http://124.72.109.183:8118',' Shttp': 'http://49.85.1.79:31666'}headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36','X-Requested-With': 'XMLHttpRequest'}try:try:try:aq = pic[num]aqq = aq.split('/')except:passif type == 'kugou' and len(aqq) - 1 == 6:aqqe = str(aqq[0]) + str('//') + str(aqq[2]) + str('/') + str(aqq[3]) + str('/') + str('400') + str('/') + str(aqq[5]) + str('/') + str(aqq[6])print(aqqe)elif type == 'netease' and len(aqq) - 1 == 4:aqn = aq.split('?')b = '?param=500x500'aqqe = (str(aqn[0]) + str(b))print(aqqe)else:aqqe = pic[num]req = get(aqqe)checkfile = open(str(data + '/ls1.png'), 'w+b')for i in req.iter_content(100000):checkfile.write(i)checkfile.close()lsfile = str(data + '/ls1.png')safile = str(data + '/back.png')draw(lsfile, safile)except:passurl1 = urls[num]print(url1)number = number + 1path = str(data + '\{}.臨時文件'.format(number))headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.110.430.128 Safari/537.36','X-Requested-With': 'XMLHttpRequest'}with get(url1, stream=True, headers=headers) as r, open(path, 'wb') as file:total_size = int(r.headers['content-length'])content_size = 0for content in r.iter_content(chunk_size=1024):file.write(content)content_size += len(content)plan = (content_size / total_size) * 100# print(int(plan))develop = str(int(plan)) + str('%')self.trigger.emit(str(develop))to = 'downloadmusic\{}.mp3'.format(songs[num])makedirs('downloadmusic', exist_ok=True)except:passtry:copyfile(path, to)except:passdownloading = Falseself.trigger.emit(str('finish'))except:????????????????self.trigger.emit(str('nofinish'))
音樂播放模塊:
def bofang(self, num, bo):print('嘗試進(jìn)行播放')try:import urllibglobal pauseglobal songsglobal musicglobal downloadingdownloading = Trueself.console_button_3.setIcon(icon('fa.pause', color='#F76677', font=18))pause = Falsetry:mixer.stop()except:passmixer.init()try:self.Timer = QTimer()self.Timer.start(500)except:passtry:self.label.setText('正在尋找文件...')self.work = WorkThread()self.work.start()self.work.trigger.connect(self.display)except:print('無法播放,歌曲下載錯誤')downloading = Falsepassexcept:sleep(0.1)print('播放系統(tǒng)錯誤')# self.next()passdef display(self, sd):global pauseglobal songedglobal urledglobal lrcdglobal timenumif sd == 'finish':try:if bo == 'boing':self.label.setText(songs[num])elif bo == 'boed':self.label.setText(songed[num])elif bo == 'love':self.label.setText(loves[num])try:pix_img = QPixmap(str(data + '/back.png'))pix = pix_img.scaled(300, 300, Qt.KeepAspectRatio)self.label5.setPixmap(pix)except:pix_img = QPixmap(str(data + '/backdown.png'))pix = pix_img.scaled(300, 300, Qt.KeepAspectRatio)self.label5.setPixmap(pix)print(str(data + '\{}.臨時文件'.format(number)))mixer.music.load(str(data + '\{}.臨時文件'.format(number))) # 載入音樂mixer.music.play()self.console_button_3.setIcon(icon('fa.pause', color='#F76677', font=18))pause = Falsetry:mp3 = str(data + '\{}.臨時文件'.format(number))xx = load(mp3)timenum = xx.info.time_secsglobal startstart = Trueexcept:print('MP3錯誤,播放失敗')if bo == 'boing':songed.append(songs[num])urled.append(urls[num])picd.append(pic[num])lrcd.append(lrcs[num])r = 0self.listwidget2.clear()for i in songed:# self.listwidget.addItem(i)#將文件名添加到listWidgetself.listwidget2.addItem(i)self.listwidget2.item(r).setForeground(Qt.white)r = r + 1else:pass# 播放音樂except:passelif sd == 'nofinish':self.label.setText('下載錯誤')elif sd == 'lrcfinish':r = 0for i in lrct:if not i == '\r':r = r + 1else:passelif sd == 'lrcnofinish':passelse:????????????self.label.setText('加速下載中,已完成{}'.format(sd))
至此,我們的音樂播放器已經(jīng)可以正常播放音樂了。
4. 附加功能
主要功能已經(jīng)完成了,下面我們還可以添加一些附加功能,例如播放方式列表循環(huán)、單曲循環(huán)、隨機(jī)播放,當(dāng)前上一首下一首播放,播放暫停開始,音量增加減少等等。
播放模式:
(1)隨機(jī)播放:
def shui(self):global numglobal songsif bo == 'boing':q = int(len(songs) - 1)num = int(randint(1, q))elif bo == 'love':q = int(len(loves) - 1)num = int(randint(1, q))else:q = int(len(songed) - 1)num = int(randint(0, q))try:print('隨機(jī)播放下一首')mixer.init()self.Timer = QTimer()self.Timer.start(500)# self.Timer.timeout.connect(self.timercontorl)#時間函數(shù),與下面的進(jìn)度條和時間顯示有關(guān)if bo == 'boing':self.label.setText(songs[num])elif bo == 'love':self.label.setText(loves[num])else:self.label.setText(songed[num])self.bofang(num, bo) # 播放音樂except:????????pass
(2) 上一首、下一首:
def last(self):global numglobal songsif num == 0:print('冇')num = len(songs) - 1else:num = num - 1try:self.bofang(num)self.label.setText(songs[num])except:pass#下一首def next(self):print ('nexting')global numglobal songsif num == len(songs) - 1:print('冇')num = 0else:num = num + 1try:self.label.setText(songs[num])self.bofang(num)except:print ('next error')????????pass
(3)單曲循環(huán):
def always(self):try:if bo == 'boing':self.label.setText(songs[num])else:self.label.setText(songed[num])self.bofang(num, bo) # 播放音樂except:????????pass
(4) 播放模式選擇:
def playmode(self):global playtry:if play == 'shun':play = 'shui'print('切換到隨機(jī)播放')self.label2.setText("當(dāng)前為隨機(jī)播放")try:self.console_button_6.setIcon(icon('fa.random', color='#3FC89C', font=18))print('done')except:print('none')pass# self.left_shui.setText('切換為單曲循環(huán)')elif play == 'shui':play = 'always'print('切換到單曲循環(huán)')self.label2.setText("當(dāng)前為單曲循環(huán)")try:self.console_button_6.setIcon(icon('fa.retweet', color='#3FC89C', font=18))print('done')except:print('none')# self.left_shui.setText('切換為順序播放')elif play == 'always':play = 'shun'print('切換到順序播放')self.label2.setText("當(dāng)前為順序播放")try:self.console_button_6.setIcon(icon('fa.align-center', color='#3FC89C', font=18))print('done')except:print('none')# self.left_shui.setText('切換為隨機(jī)播放')except:print('模式選擇錯誤')????????pass
播放暫停/開始:
def pause(self):global pauseif pause:try:mixer.music.unpause()except:passself.console_button_3.setIcon(icon('fa.pause', color='#3FC89C', font=18))pause = Falseelse:try:mixer.music.pause()except:passself.console_button_3.setIcon(icon('fa.play', color='#F76677', font=18))????????pause?=?True
音量提高/降低:
def voiceup(self):try:print('音量加大')global voicevoice += 0.1if voice > 1:voice = 1mixer.music.set_volume(voice)k = Decimal(voice).quantize(Decimal('0.00'))self.label3.setText('音量:{}'.format(str(k * 100) + '%'))except:passdef voicedown(self):try:print('音量減少')global voicevoice -= 0.1if voice < 0:voice = 0mixer.music.set_volume(voice)k = Decimal(voice).quantize(Decimal('0.00'))self.label3.setText('音量:{}'.format(str(k * 100) + '%'))except:????????pass
至此,這款音樂播放器就基本完成啦~ 一起來看看效果吧!
三、結(jié)束語
當(dāng)然這款音樂播放器還有待完善的功能尚未完成:
音樂本地下載保存
播放本地音樂
添加我喜愛的音樂功能
歌詞播放
音樂進(jìn)度跳播
后面有時間這些功能也會繼續(xù)完善下!今天我們就到這里,明天繼續(xù)努力!
- EOF -
? 推薦閱讀:
點擊關(guān)注【python之禪】,提升Python技能??????
