用python写QQ音乐播放器
时间: 2023-06-30 22:18:47 浏览: 220
基于 electron-vue 开发的音乐播放器,界面模仿QQ音乐,技术栈electron-vue+vue+vuex+vue-r
5星 · 资源好评率100%
要使用Python编写QQ音乐播放器,你需要掌握以下几个步骤:
1. 安装必要的库:你需要安装PyQt5和Requests库。PyQt5是Python的GUI框架,Requests库用于从QQ音乐API获取音乐歌曲和封面图片。
2. 获取QQ音乐歌曲信息:从QQ音乐API获取音乐歌曲信息,包括歌曲名、歌手、专辑、歌曲链接和封面图片等。
3. 创建播放器界面:使用PyQt5创建音乐播放器的界面,包括播放、暂停、上一首、下一首和音量控制等按钮。
4. 播放音乐:使用Python的PyGame库播放音乐,将获取到的音乐链接传递给PyGame库进行播放。
下面是一个简单的示例代码:
```python
import sys
import requests
import pygame
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QSlider
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QSize
class QQMusicPlayer(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
self.music_list = []
self.current_music_index = 0
self.playing = False
self.volume = 50
pygame.init()
def init_ui(self):
self.resize(400, 300)
self.setWindowTitle('QQ Music Player')
self.setWindowFlags(Qt.WindowCloseButtonHint)
self.title_label = QLabel('QQ Music Player')
self.title_label.setAlignment(Qt.AlignCenter)
self.album_cover_label = QLabel(self)
self.album_cover_label.setFixedSize(200, 200)
self.album_cover_label.setPixmap(QPixmap('cover.jpg').scaled(QSize(200, 200), Qt.KeepAspectRatio))
self.song_name_label = QLabel('No song')
self.song_name_label.setAlignment(Qt.AlignCenter)
self.singer_label = QLabel('No singer')
self.singer_label.setAlignment(Qt.AlignCenter)
self.play_button = QPushButton('Play')
self.play_button.clicked.connect(self.play_pause_music)
self.prev_button = QPushButton('Prev')
self.prev_button.clicked.connect(self.play_prev_music)
self.next_button = QPushButton('Next')
self.next_button.clicked.connect(self.play_next_music)
self.volume_slider = QSlider(Qt.Horizontal)
self.volume_slider.setMinimum(0)
self.volume_slider.setMaximum(100)
self.volume_slider.setValue(50)
self.volume_slider.valueChanged.connect(self.change_volume)
vbox_layout = QVBoxLayout()
vbox_layout.addWidget(self.title_label)
vbox_layout.addWidget(self.album_cover_label)
vbox_layout.addWidget(self.song_name_label)
vbox_layout.addWidget(self.singer_label)
hbox_layout = QHBoxLayout()
hbox_layout.addWidget(self.prev_button)
hbox_layout.addWidget(self.play_button)
hbox_layout.addWidget(self.next_button)
vbox_layout.addLayout(hbox_layout)
vbox_layout.addWidget(self.volume_slider)
self.setLayout(vbox_layout)
def get_music_list(self, keyword):
url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?new_json=1&remoteplace=txt.yqq.song&searchid=70837171441911197&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w=' + keyword
headers = {
'referer': 'https://y.qq.com/portal/search.html'
}
response = requests.get(url, headers=headers)
json_data = response.json()
music_list = []
for item in json_data['data']['song']['list']:
music = {
'name': item['name'],
'singer': item['singer'][0]['name'],
'album': item['album']['name'],
'cover': item['album']['mid'],
'url': self.get_music_url(item['mid'])
}
music_list.append(music)
self.music_list = music_list
self.current_music_index = 0
self.update_ui()
def get_music_url(self, mid):
url = 'https://u.y.qq.com/cgi-bin/musicu.fcg?callback=getplaysongvkey5340713917016285&g_tk=5381&jsonpCallback=getplaysongvkey5340713917016285&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%227320798373%22%2C%22songmid%22%3A%5B%22' + mid + '%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%220%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A0%2C%22format%22%3A%22json%22%2C%22ct%22%3A20%2C%22cv%22%3A0%7D%7D'
headers = {
'referer': 'https://y.qq.com/'
}
response = requests.get(url, headers=headers)
json_data = response.json()
return 'http://isure.stream.qqmusic.qq.com/' + json_data['req_0']['data']['midurlinfo'][0]['purl']
def update_ui(self):
if len(self.music_list) == 0:
self.song_name_label.setText('No song')
self.singer_label.setText('No singer')
self.album_cover_label.setPixmap(QPixmap('cover.jpg').scaled(QSize(200, 200), Qt.KeepAspectRatio))
else:
music = self.music_list[self.current_music_index]
self.song_name_label.setText(music['name'])
self.singer_label.setText(music['singer'])
url = 'https://y.gtimg.cn/music/photo_new/T002R300x300M000' + music['cover'] + '.jpg'
response = requests.get(url)
image_data = response.content
pixmap = QPixmap()
pixmap.loadFromData(image_data)
self.album_cover_label.setPixmap(pixmap.scaled(QSize(200, 200), Qt.KeepAspectRatio))
def play_pause_music(self):
if len(self.music_list) == 0:
return
if self.playing:
pygame.mixer.music.pause()
self.play_button.setText('Play')
self.playing = False
else:
pygame.mixer.music.unpause()
self.play_button.setText('Pause')
self.playing = True
def play_prev_music(self):
if len(self.music_list) == 0:
return
self.current_music_index = (self.current_music_index - 1) % len(self.music_list)
self.play_music()
def play_next_music(self):
if len(self.music_list) == 0:
return
self.current_music_index = (self.current_music_index + 1) % len(self.music_list)
self.play_music()
def play_music(self):
music = self.music_list[self.current_music_index]
pygame.mixer.music.load(music['url'])
pygame.mixer.music.set_volume(self.volume / 100)
pygame.mixer.music.play()
self.play_button.setText('Pause')
self.playing = True
self.update_ui()
def change_volume(self, value):
self.volume = value
pygame.mixer.music.set_volume(self.volume / 100)
if __name__ == '__main__':
app = QApplication(sys.argv)
player = QQMusicPlayer()
player.show()
sys.exit(app.exec_())
```
这是一个初级版本的QQ音乐播放器,它可以通过关键字搜索QQ音乐中的歌曲,并播放它们。你可以根据自己的需要对代码进行修改和扩展。
阅读全文