当前位置:网站首页>Minimalist random music player
Minimalist random music player
2022-06-10 12:14:00 【Yangzongde】
Minimalist random music player
explain
The main function :
1. Online playing of music
2. Connected to Netease cloud music engine
Software architecture :
- Player use PyQt5 To write
- The music interface comes from UomgAPI(https://api.uomg.com/), Connected to Netease cloud music engine
Main technical route : - Random access to music through the music engine
- use PyQt5 Play music
- Control the music playing through the form
Main frame : - python
- PyQt5
- requests
Work :
1.python Form development 、 Style adjustment
2. Form music control : Play 、 The following piece 、 Progress bar display, etc
3. Call the music interface , send out post Interface request obtain Music links
4. call PyQt5 How to play it , Play music
5. Get the music playing duration , Write timer , When playback is complete , Get a link to play the next music , And then play it .
effect

Part of the code
Complete code download Go to this link
https://download.csdn.net/download/qq_35385687/85491514
# coding:utf-8
# Minimalist random music player
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtMultimedia import QMediaContent,QMediaPlayer
# from PySide2 import QtWidgets
# from PySide2 import QtGui
# from PySide2 import QtCore
# from PySide2.QtMultimedia import QMediaContent
# from PySide2.QtMultimedia import QMediaPlayer
import qtawesome as qta
import requests,traceback
class Music(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(400,200)
self.setWindowTitle(" Minimalist random music player ")
self.init_ui()
self.custom_style()
self.playing = False # The playback status is initialized to No
self.player = QMediaPlayer(self)
self.timer = QtCore.QTimer()
self.timer.setInterval(1000)
self.timer.start()
self.timer.timeout.connect(self.check_music_status)
# Set the style
def custom_style(self):
self.setStyleSheet(''' #main_widget{ border-radius:5px; } #play_btn,#pervious_btn,#next_btn{ border:none; } #play_btn:hover,#pervious_btn:hover,#next_btn:hover{ background:gray; border-radius:5px; cursor:pointer; } ''')
self.close_btn.setStyleSheet(''' QPushButton{ background:#F76677; border-radius:5px; } QPushButton:hover{ background:red; }''')
self.status_label.setStyleSheet(''' QLabel{ background:#F7D674; border-radius:5px; } ''')
# initialization UI Interface
def init_ui(self):
# Window layout
self.main_widget = QtWidgets.QWidget()
self.main_widget.setObjectName("main_widget")
self.main_layout = QtWidgets.QGridLayout()
self.main_widget.setLayout(self.main_layout)
# title
self.title_lable = QtWidgets.QLabel(" Minimalist random music player ")
# close button
self.close_btn = QtWidgets.QPushButton("") # close button
self.close_btn.clicked.connect(self.close_btn_event)
self.close_btn.setFixedSize(15,15)
# Music status button
self.status_label = QtWidgets.QLabel("")
# self.swith_btn.clicked.connect(self.swith_background)
self.status_label.setFixedSize(15,15)
# Play button
play_icon = qta.icon("fa.play-circle",)
self.play_btn = QtWidgets.QPushButton(play_icon,"")
self.play_btn.setIconSize(QtCore.QSize(80, 80))
self.play_btn.setFixedSize(82,82)
self.play_btn.setObjectName("play_btn")
self.play_btn.clicked.connect(self.play_music)
# Next button
next_icon = qta.icon("fa.play-circle-o")
self.next_btn = QtWidgets.QPushButton(next_icon,"")
self.next_btn.setIconSize(QtCore.QSize(80,80))
self.next_btn.setFixedSize(82,82)
self.next_btn.setObjectName("next_btn")
self.next_btn.clicked.connect(self.next_music)
# Progress bar
self.process_bar = QtWidgets.QProgressBar()
self.process_value = 0
self.process_bar.setValue(self.process_value)
self.process_bar.setFixedHeight(5)
self.process_bar.setTextVisible(False)
self.main_layout.addWidget(self.close_btn,0,0,1,1)
self.main_layout.addWidget(self.title_lable,0,1,1,1)
self.main_layout.addWidget(self.status_label,1,0,1,1)
self.main_layout.addWidget(self.play_btn, 1, 1, 1, 1)
self.main_layout.addWidget(self.next_btn, 1, 2, 1, 1)
self.main_layout.addWidget(self.process_bar,2,0,1,3)
self.setCentralWidget(self.main_widget)
# self.setWindowOpacity(0.9) # Set window transparency
# self.setAttribute(QtCore.Qt.WA_TranslucentBackground) # Set window background transparency
self.setWindowFlag(QtCore.Qt.FramelessWindowHint) # Hide borders
# Shut down the program
def close_btn_event(self):
self.close()
# Mouse long press event
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = event.globalPos() - self.pos()
event.accept()
self.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
# Mouse movement events
def mouseMoveEvent(self, QMouseEvent):
if QtCore.Qt.LeftButton and self.m_drag:
self.move(QMouseEvent.globalPos() - self.m_DragPosition)
QMouseEvent.accept()
# Mouse release event
def mouseReleaseEvent(self, QMouseEvent):
self.m_drag = False
self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
# Play music
def play_music(self):
try:
# Play music
if self.playing is False:
self.playing = True # Set the playback status to yes
self.play_btn.setIcon(qta.icon("fa.pause-circle")) # Set the playback icon
player_status = self.player.mediaStatus() # Get player status
# print(" Current playback status :",player_status)
if player_status == 6:
# Set the status label to green
self.status_label.setStyleSheet('''QLabel{background:#6DDF6D;border-radius:5px;}''')
self.player.play()
else:
self.next_music()
# Music suspended
else:
# Set the status to blue
self.status_label.setStyleSheet('''QLabel{background:#0099CC;border-radius:5px;}''')
self.playing = False
self.play_btn.setIcon(qta.icon("fa.play-circle"))
self.player.pause()
except Exception as e:
print(repr(e))
# Next music
def next_music(self):
try:
# Set the status to yellow
self.status_label.setStyleSheet(''' QLabel{ background:#F7D674; border-radius:5px; } ''')
self.playing = True # Set the playback status to yes
self.play_btn.setIcon(qta.icon("fa.pause-circle")) # Modify the playback icon
self.process_value = 0 # Reset progress value
# Get network songs
self.get_music_thread = GetMusicThread()
self.get_music_thread.finished_signal.connect(self.init_player)
self.get_music_thread.start()
except Exception as e:
print(traceback.print_exc())
# Set up the player
def init_player(self,url):
# print(" Get links to music :",url)
content = QMediaContent(QtCore.QUrl(url))
self.player.setMedia(content)
self.player.setVolume(50)
self.player.play()
self.duration = self.player.duration() # Length of music
# Set the status to green
self.status_label.setStyleSheet(''' QLabel{ background:#6DDF6D; border-radius:5px; } ''')
# Progress bar timer
self.process_timer = QtCore.QTimer()
self.process_timer.setInterval(1000)
self.process_timer.start()
self.process_timer.timeout.connect(self.process_timer_status)
# Timer
def check_music_status(self):
player_status = self.player.mediaStatus()
player_duration = self.player.duration()
# print(" Music time :",player_duration)
# print(" Current player status ",player_status)
if player_status == 7:
self.next_music()
if player_duration > 0:
self.duration = player_duration
# Progress bar timer
def process_timer_status(self):
try:
if self.playing is True:
self.process_value += (100 / (self.duration/1000))
# print(" Current progress :",self.process_value)
self.process_bar.setValue(self.process_value)
except Exception as e:
print(repr(e))
# Different step sub thread obtains music link
class GetMusicThread(QtCore.QThread):
finished_signal = QtCore.pyqtSignal(str)
# finished_signal = QtCore.Signal(str) # PySide2 How to use
def __init__(self,parent=None):
super().__init__(parent)
def run(self):
reps = requests.post("https://api.uomg.com/api/rand.music?format=json")
# print(reps.json())
file_url = reps.json()['data']['url']
self.finished_signal.emit(file_url)
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
gui = Music()
gui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
边栏推荐
- 【严选】,真题解析
- 极简随机音乐播放器
- IO文件流重复读取
- 0509-130 Symbol resolution failed for /oracle/app/oracle/12.2.0/db_ 1/lib/libons. So problem handling
- (8) Initialization list
- 【云图说】每个成功的业务系统都离不开APIG的保驾护航
- 漏扫工具学习笔记
- ShaderGraph——水晶
- C Introduction à la Bibliothèque des meilleures pratiques linguistiques (Partie 2)
- Zipoutputstream use
猜你喜欢
随机推荐
【严选】,真题解析
Web design and development, efficient web development
MAX3051的can芯片的学习
移动端整页滑屏示例(上下滑动整屏)(整理)
绿盟数据库防火墙(DAS-FW)获得鲲鹏Validated认证
SQL查询结果添加行号字段----sqlserver
0509-130 Symbol resolution failed for /oracle/app/oracle/12.2.0/db_ 1/lib/libons. So problem handling
Collected data, must see
4.25 million budget bidding: centralized procurement of onsite maintenance services for Oracle, teledb, telepg and other core system databases of Jiangsu Telecom
2022 年 5 月产品大事记
Gimp - free and open source image processing software with powerful functions, known as an excellent substitute for Photoshop
【总结向】个人赛补题 POJ - 3041 Asteroids &CodeForces - 173B Chamber of Secrets
IO文件流重复读取
'getColor(int)' is deprecated ,getColor过时
ShaderGraph——302游动的龙
期货开户在网上办理安全吗?靠谱吗?
International Archives day 𞓜 Hangao database enables digital archives construction with science and technology
Do not concatenate text displayed with setText,use resource string with placeholders.
共筑数字经济可信底座|2022可信数据库峰会即将召开
SQL Server AlwaysOn viewing data synchronization progress









