当前位置:网站首页>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 :

  1. Player use PyQt5 To write
  2. The music interface comes from UomgAPI(https://api.uomg.com/), Connected to Netease cloud music engine
    Main technical route :
  3. Random access to music through the music engine
  4. use PyQt5 Play music
  5. Control the music playing through the form
    Main frame :
  6. python
  7. PyQt5
  8. 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

 Insert picture description here

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()
原网站

版权声明
本文为[Yangzongde]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101207163663.html