SoFunction
Updated on 2024-11-13

Python to write a minimalist music player function using PyQt5/PySide2

The epidemic is raging, suffocating at home is really boring, so I just write something, so there is this extremely extremely extremely minimalist music player.

This extremely minimalist music player is similar to the "Burn After Reading" software, the player can randomize the song, get the next song, can not replay the previous song, can not get the name of the song and the singer. You can't replay the previous song, you can't get the name of the song or the artist. The songs you've listened to are just like a cloud of smoke, you can't play them.

Here's how to implement this music player software in Python!

I. Creating the UI interface

First, let's create the UI of this player. This player has a total of 6 controls:

  1. Program close button in the upper left corner;
  2. Playback Status tab on the left side;
  3. Slogan at the top.
  4. Play/Pause button;
  5. Next song button;
  6. Play the progress bar;

Based on the above controls, we use a grid layout to arrange them;

class Music():
 def __init__(self):
 super().__init__()
 (400,200)
 ("Mr. State.")
 self.init_ui()
 self.custom_style()
  = False # Playback state initialized to no
  = QMediaPlayer(self)

 # Set the style
 def custom_style(self):
 ('''
  #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;
  }
 ''')

 # Initialize the UI interface
 def init_ui(self):
 # Window layout
 self.main_widget = ()
 self.main_widget.setObjectName("main_widget")
 self.main_layout = ()
 self.main_widget.setLayout(self.main_layout)

 # Title
 self.title_lable = ("Hearers, like passing clouds ......")

 # Close button
 self.close_btn = ("") # Close button
 self.close_btn.setFixedSize(15,15)

 # Music status button
 self.status_label = ("")
 self.status_label.setFixedSize(15,15)

 # Play button
 play_icon = ("-circle",)
 self.play_btn = (play_icon,"")
 self.play_btn.setIconSize((80, 80))
 self.play_btn.setFixedSize(82,82)
 self.play_btn.setObjectName("play_btn")

 # Next song button
 next_icon = ("-circle-o")
 self.next_btn = (next_icon,"")
 self.next_btn.setIconSize((80,80))
 self.next_btn.setFixedSize(82,82)
 self.next_btn.setObjectName("next_btn")

 # Progress bars
 self.process_bar = ()
 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.main_widget)

In the above code, we use Qt's built-in button control QPushButton, label control QLabel, progress bar control QProgressBar and so on to build up the basic interface of the player, and then through the setStyleSheet() method to set the style of each control, and finally get the following interface:

The top border is kind of ugly, we hide it with setWindowFlag():

() # Hide Borders

This completes the complete and good looking interface as shown below:

II. Obtaining songs on the Internet

Since it is a music player, of course it should be able to play music. This music player we created can't add local music, so it has to play music from the web. Mr. State () looked for a long time on the Internet, and finally found a free and stable music interface. Requesting this interface will randomly return a music playback address. It just fits our player's settings, so here it is:

def run(self):
 reps = ("/api/?format=json")
 # print(())
 file_url = ()['data']['url']
 self.finished_signal.emit(file_url)

We wrap it in Qt's threading module QThread and call it as an asynchronous subthread:

With the UI interface and web music, the following controls on our UI interface are connected to the music playback via signal slots (event responses).

III. Creating and linking signal slots

Since we are hiding the UI interface's own border, there is no more close button. We previously created the little red dot on the left side as a button to close the player, and then we still need a response function to close the player:

# Close the program
def close_btn_event(self):
 ()
self.close_btn.(self.close_btn_event)

In this way, the player can be closed and exited by clicking on the little red dot. Here's how to handle music playback.

Here, we use the QtMultimedia submodule provided by Qt QMediaPlayer to realize the MP3 music playback, QtMultimedia can be seen in Qt's official documentation: /qtforpython/PySide2/QtMultimedia/#:

This method, accepts a network address parameter for MP3 music and then configures it to play it.

We have created a GetMusicThread class that inherits from QThread, and we can get and play songs by creating another method that calls it and connecting the completion signal (finished_signal) to the init_player() method:

Here, the next_music() method calls GetMusicThread to get a random song, and then returns the URL address of the song to the init_player() method for playback.

The next_music() method is the main method for playing a song, it can be called by the play button, the next button, and automatically playing the next song after a song has finished playing.

The Play Music button is needed to control and determine the status of the music, which can be obtained through the QMediaPlayer's mediaStatus() method and used to do so:

In order to be able to automatically fetch and play the next song after a song has been played, we need to create a timer that will fetch the state of the current player every second to determine if it has finished playing the music, and call the next_music() method if it has:

 = ()
(1000)
()
(self.check_music_status)
def check_music_status(self):
 player_status = ()
 player_duration = ()
 # print("Music duration: ",player_duration)
 # print("Current player status",player_status)
 if player_status == 7:
 self.next_music()

With a few more tweaks, we've completed this "listen and burn" web music player:

The full code can be headed to Mr. State's repository on Gitee (/zmister/yunyan_music )

summarize

The above is a small introduction to the use of PyQt5/PySide2 to write a minimalist music player function, I hope to help you!