




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
第自己用python做的一款超炫酷音樂播放器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()#播放進度部件
self.right_process_bar.setValue(49)
self.right_process_bar.setFixedHeight(3)#設(shè)置進度條高度
self.right_process_bar.setTextVisible(False)#不顯示進度條文字
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)鍵字和音樂來源進行音樂爬取。這里我們需要通過多線程,將歌曲、歌手、歌曲url地址全都獲取。核心代碼如下:
defrun(self):
qmut.lock()
try:
globalpaing
globalstop
globallrcs
globalurls
globalsongs
globalname
globalsongid
globalproxies
globalpic
globaltryed
paing=True
print('搜索軟件{}'.format(type))
print('開始搜索')
name=name
headers={
'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/121.110.430.128Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
urls=[]
songs=[]
pic=[]
lrcs=[]
pages=5
print(pages)
forainrange(0,pages):
ifnotstop:
urlss=['/','/dmusic/','/',
'http://music.laomao.me/']
print(tryed)
iftryed3:
tryed=0
url=urlss[tryed]
else:
url=urlss[tryed]
print(urlss[tryed])
params={'input':name,
'filter':'name',
'type':type,
'page':a
ifnotstop:
try:
res=post(url,params,headers=headers,proxies=proxies)
html=res.json()
foriinrange(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:
pass
except:
stop=False
paing=False
self.trigger.emit(str('finish'))
else:
self.trigger.emit(str('finish'))
else:
self.trigger.emit(str('clear'))
pass
stop=False
paing=False
except:
print('爬取歌曲出錯')
self.trigger.emit(str('unfinish'))
stop=False
paing=False
qmut.unlock()
爬取代碼寫好了,我們還需要將爬取的這些音樂數(shù)據(jù)列表顯示到畫面中搜索頁面中。代碼如下:
defrepite(self,name,type):
globaltryed
globalpaing
globalstop
self.listwidget.clear()
self.listwidget.addItem('搜索中')
self.listwidget.item(0).setForeground(Qt.white)
try:
ifpaing:
stop=True
self.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+1
get_info('/free/inha')
self.listwidget.addItem('貌似沒網(wǎng)了呀`(*﹏*)′,再試一遍吧~')
self.listwidget.item(0).setForeground(Qt.white)
效果如下:
3.音樂播放
關(guān)鍵字搜索音樂列表完成之后,接下來我們需要實現(xiàn)音樂播放功能。首先需要從爬取的url下載待播放的歌曲,這里還是通過多線程進行。核心代碼如下:
classWorkThread(QThread):
trigger=pyqtSignal(str)
def__int__(self):
#初始化函數(shù)
super(WorkThread,self).__init__()
#進度條
defcbk(self,a,b,c):
'''''回調(diào)函數(shù)
@a:已經(jīng)下載的數(shù)據(jù)塊
@b:數(shù)據(jù)塊的大小
@c:遠(yuǎn)程文件的大小
per=100.0*a*b/c
ifper100:
per=100
#print('%.2f%%'%per)
self.trigger.emit(str('%.2f%%'%per))
defrun(self):
try:
globalnumber
globalpath
globaldownloading
try:
proxies={
'http':'83:8118',
'Shttp':'9:31666'
headers={
'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/86.0.4240.198Safari/537.36',
'X-Requested-With':'XMLHttpRequest'}
try:
try:
try:
aq=pic[num]
aqq=aq.split('/')
except:
pass
iftype=='kugou'andlen(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)
eliftype=='netease'andlen(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')
foriinreq.iter_content(100000):
checkfile.write(i)
checkfile.close()
lsfile=str(data+'/ls1.png')
safile=str(data+'/back.png')
draw(lsfile,safile)
except:
pass
url1=urls[num]
print(url1)
number=number+1
path=str(data+'\{}.臨時文件'.format(number))
headers={
'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/121.110.430.128Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
withget(url1,stream=True,headers=headers)asr,open(path,'wb')asfile:
total_size=int(r.headers['content-length'])
content_size=0
forcontentinr.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:
pass
try:
copyfile(path,to)
except:
pass
downloading=False
self.trigger.emit(str('finish'))
except:
self.trigger.emit(str('nofinish'))
音樂播放模塊:
defbofang(self,num,bo):
print('嘗試進行播放')
try:
importurllib
globalpause
globalsongs
globalmusic
globaldownloading
downloading=True
self.console_button_3.setIcon(icon('fa.pause',color='#F76677',font=18))
pause=False
try:
mixer.stop()
except:
pass
mixer.init()
try:
self.Timer=QTimer()
self.Timer.start(500)
except:
pass
try:
self.label.setText('正在尋找文件...')
self.work=WorkThread()
self.work.start()
self.work.trigger.connect(self.display)
except:
print('無法播放,歌曲下載錯誤')
downloading=False
pass
except:
sleep(0.1)
print('播放系統(tǒng)錯誤')
#self.next()
pass
defdisplay(self,sd):
globalpause
globalsonged
globalurled
globallrcd
globaltimenum
ifsd=='finish':
try:
ifbo=='boing':
self.label.setText(songs[num])
elifbo=='boed':
self.label.setText(songed[num])
elifbo=='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=False
try:
mp3=str(data+'\{}.臨時文件'.format(number))
xx=load(mp3)
timenum=.time_secs
globalstart
start=True
except:
print('MP3錯誤,播放失敗')
ifbo=='boing':
songed.append(songs[num])
urled.append(urls[num])
picd.append(pic[num])
lrcd.append(lrcs[num])
r=0
self.listwidget2.clear()
foriinsonged:
#self.listwidget.addItem(i)#將文件名添加到listWidget
self.listwidget2.addItem(i)
self.listwidget2.item(r).setForeground(Qt.white)
r=r+1
else:
pass
#播放音樂
except:
pass
elifsd=='nofinish':
self.label.setText('下載錯誤')
elifsd=='lrcfinish':
r=0
foriinlrct:
ifnoti=='\r':
r=r+1
else:
pass
elifsd=='lrcnofinish':
pass
else:
self.label.setText('加速下載中,已完成{}'.format(sd))
至此,我們的音樂播放器已經(jīng)可以正常播放音樂了。
4.附加功能
主要功能已經(jīng)完成了,下面我們還可以添加一些附加功能,例如播放方式列表循環(huán)、單曲循環(huán)、隨機播放,當(dāng)前上一首下一首播放,播放暫停開始,音量增加減少等等。
播放模式:
(1)隨機播放:
defshui(self):
globalnum
globalsongs
ifbo=='boing':
q=int(len(songs)-1)
num=int(randint(1,q))
elifbo=='love':
q=int(len(loves)-1)
num=int(randint(1,q))
else:
q=int(len(songed)-1)
num=int(randint(0,q))
try:
print('隨機播放下一首')
mixer.init()
self.Timer=QTimer()
self.Timer.start(500)
#self.Timer.timeout.connect(self.timercontorl)#時間函數(shù),與下面的進度條和時間顯示有關(guān)
ifbo=='boing':
self.label.setText(songs[num])
elifbo=='love':
self.label.setText(loves[num])
else:
self.label.setText(songed[num])
self.bofang(num,bo)#播放音樂
except:
pass
(2)上一首、下一首:
deflast(self):
globalnum
globalsongs
ifnum==0:
print('冇')
num=len(songs)-1
else:
num=num-1
try:
self.bofang(num)
self.label.setText(songs[num])
except:
pass
defnext(self):
print('nexting')
globalnum
globalsongs
ifnum==len(songs)-1:
print('冇')
num=0
else:
num=num+1
try:
self.label.setText(songs[num])
self.bofang(num)
except:
print('nexterror')
pass
(3)單曲循環(huán):
defalways(self):
try:
ifbo=='boing':
self.label.setText(songs[num])
else:
self.label.setText(songed[num])
self.bofang(num,bo)#播放音樂
except:
pass
(4)播放模式選擇:
defplaymode(self):
globalplay
try:
ifplay=='shun':
play='shui'
print('切換到隨機播放')
self.label2.setText("當(dāng)前為隨機播放")
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)')
elifplay=='shui':
play='always'
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 水利工程拆裝與施工管理合同
- 車貸解除合同范本
- 破產(chǎn)重整過程中的財稅處理咨詢合同
- 生物科技股東退股及競業(yè)限制合同
- 交通事故車輛過戶調(diào)解合同
- 倉儲物流設(shè)施采購合同類型及安全標(biāo)準(zhǔn)
- 財務(wù)總監(jiān)財務(wù)信息化建設(shè)勞動合同
- 股權(quán)投資居間服務(wù)及稅收籌劃合同
- 財務(wù)管理培訓(xùn)與內(nèi)部控制系統(tǒng)建設(shè)合同
- 低碳環(huán)保廠房物業(yè)管理與綠色能源應(yīng)用合同
- 外科學(xué)第七版周圍血管和淋巴管疾病
- 安全生產(chǎn)試題庫看圖找錯課件
- 二級綜合醫(yī)院基本標(biāo)準(zhǔn)(2021年版)
- 北京市初中學(xué)業(yè)水平考試體育與健康知識模擬練習(xí)題(含答案)
- 市政工程質(zhì)量通病與防治
- 配電項目工程重點、難點及解決措施
- 北京理工大學(xué)出版社二年級下冊《勞動》教案
- JJG 966-2010手持式激光測距儀
- GB/T 26659-2011鑄造用再生硅砂
- GB/T 21558-2008建筑絕熱用硬質(zhì)聚氨酯泡沫塑料
- GB/T 18494.1-2014變流變壓器第1部分:工業(yè)用變流變壓器
評論
0/150
提交評論