一起用python做个炫酷音乐播放器,想听啥随便搜( 三 )

喜爱的歌初始化读取:
重新运行音乐播放器时,我们需要加载保存在本地的喜爱歌曲数据,并将歌曲列表信息加载显示到喜爱的歌子列表页面中 。核心代码如下:
def run(self):try:apdataas = getenv("APPDATA")filepathas = '{}/musicdata'.format(apdataas)global lovelrcglobal loveurlsglobal lovesglobal lovepicsglobal voice# 读取历史数据开始try:with open(filepathas + "/voice", 'r', encoding='utf-8') as f:a = f.read()# print(a)voice = float(a)print(voice)self.trigger.emit(str('voicedone'))except:self.trigger.emit(str('voicedone'))passwith open(filepathas + "/loves", 'r', encoding='utf-8') as f:a = f.read()print(a)strer = aloves = literal_eval(strer)with open(filepathas + "/lovepics", 'r', encoding='utf-8') as f:a = f.read()print(a)strer = alovepics = literal_eval(strer)with open(filepathas + "/loveurls", 'r', encoding='utf-8') as f:a = f.read()print(a)strer = aloveurls = literal_eval(strer)with open(filepathas + "/lovelrc", 'r', encoding='utf-8') as f:a = f.read()print(a)strer = alovelrc = literal_eval(strer)self.trigger.emit(str('login'))print(loves)print('read finish')except:print('read error')pass# 读取数据结束# 下载喜爱的歌列表中首项的歌曲封面try:req = get(lovepics[0])checkfile = open(str(data + '/ls3.png'), 'w+b')for i in req.iter_content(100000):checkfile.write(i)checkfile.close()lsfile = str(data + '/ls3.png')safile = str(data + '/first.png')draw(lsfile, safile)self.trigger.emit(str('first'))except:self.trigger.emit(str('nofirst'))pass列表播放:
对于加载的喜爱歌曲列表,我们可以对歌曲进行列表一键播放 。
# 音乐播放def bofang(self, num, bo):print('尝试进行播放')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('播放系统错误')pass# 播放所有歌曲 def playall(self, typer):global numglobal botry:bo = typernum = 0self.bofang(bo, num)except:print('playall error')pass # 播放喜爱歌曲def allplaylove(self):self.playall('love')4. 歌词显示对于播放的歌曲,我们可以进行歌词打印显示 。核心代码如下:
def run(self): if bo == 'love':try:proxies = {'http': 'http://124.72.109.183:8118','http': '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 = lovepics[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 = lovepics[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)picno = Trueexcept:print('图片错误')picno = Falsepassurl1 = loveurls[num]print(url1)# os.makedirs('music', exist_ok=True)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):if not stopdown: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))else:print ('down')breakstopdown = Falseto = 'downloadmusic{}.mp3'.format(songed[num])makedirs('downloadmusic', exist_ok=True)except:self.trigger.emit(str('nofinish'))passtry:lrct = []f = lovelrc[num]# 按行读取# print(f)lines = f.split('n')# print(lines)for i in lines:line1 = i.split('[')try:line2 = line1[1].split(']')if line2 == '':passelse:linew = line2[1]# print(linew)lrct.append(linew)self.trigger.emit(str('lrcfinish'))except:print('歌词错误')except:passtry:copyfile(path, to)except:passdownloading = Falseself.trigger.emit(str('finish'))except:self.trigger.emit(str('nofinish'))


推荐阅读