Foreword: We have learned a lot of knowledge points in the previous lessons, welcome everyone to go to the archeology
Today we will learn to load music and make a music player. The interface functions include:
- Load background cover
- previous song
- start\pause
- next song
- replay
- Shuffle Playback
- fast forward
Effect display (GIF so you can’t hear the sound)
Analysis
pygame.mixer.music.load(song) # load music
pygame.mixer.music.set_volume(0.5) # Set the volume of the player, parameters: 0-1, remember to write smaller, so as not to sound too loud
'''
The first parameter 1 means to play once, 0 means to play in a loop
time: refers to the number of milliseconds from which to start playing
pygame.mixer.music.play(1, time)
'''
pygame.mixer.music.play() # play
pygame.mixer.music.pause()
pygame.mixer.music.unpause()
'''
The first parameter 1 means to play once, 0 means to play in a loop
time: refers to the number of milliseconds from which to start playing
pygame.mixer.music.play(1, time)
'''
pygame.mixer.music.play(1, time)
-
Shuffle
- randon.randint(number of music)
Precautions and solutions
-
1, start at the same position as the icon of the tentative button, set the variable to switch back and forth
-
2. The first song is currently playing, click the previous one to switch to the last song; play to the last song, click the next one, it should switch to the first song
-
3. Every time a music is played, it should automatically jump to the next one
Material acquisition (function icon, music cover, music can only be downloaded by yourself): click me
Directory structure
file\folder name |
function |
image2 |
Player interface function icon picture storage |
music |
store music |
Cover |
store music cover |
Full version code:
import os
import pygame, random, sys, time
pygame.init()
# create window
sc = pygame.display.set_mode((300, 250))
# load image
bg = pygame.image.load("image2/bg.png") # background
pre_song = pygame.image.load("image2/previous.png") # previous song
start = pygame.image.load("image2/play.png") # play
pause = pygame.image.load("image2/pause.png") # pause
next_song = pygame.image.load("image2/next song.png") # next song
ff = pygame.image.load("image2/fast forward.png") # fast forward
res = pygame.image.load("image2/replay.png") # replay
rand = pygame.image.load("image2/shuffle.png") # random
# Load music playback picture
bg_list = os.listdir("./cover/")
bg_list1 = []
for bg in bg_list:
bg_list1.append(pygame.image.load("./cover/" + bg))
# The initial button is "Start" state
btn = start
# Set btn_flag to control the shape of the pause/start button
btn_flag = 0
# 9 Save the name of the song in the list. The encrypted name on the client side is very long. It is best to wrap and align each song name for easy reference
song_list = ['./music/' + i for i in os.listdir('./music/')]
# load music
index = 0
song = song_list[index]
pygame.mixer.music.load(song)
# Control the volume, the value range is a floating point number from 0-1.0. 0 is the minimum value and 1 is the maximum value.
pygame.mixer.music.set_volume(0.5)
# Play the first music, remind students here to turn down the volume of the computer to prevent the sound from suddenly being too loud to damage hearing
pygame.mixer.music.play()
time = 0
print(time)
length = len(song_list)
while True: # Add a loop to lead to events
# If it is an even number, the button switches to "start", if it is an odd number, it switches to "stop".
if btn_flag % 2 == 0:
btn = start
# Pause the music
pygame.mixer.music.pause()
else:
btn = pause
# Beginning of the music
pygame.mixer.music.unpause()
# draw background, button
sc.blit(bg_list1[index],(0,0)) # Draw the background image of the currently playing music
sc.blit(pre_song, (20, 90))
sc. blit(next_song, (210, 90))
sc.blit(rand, (115, 170))
sc. blit(btn, (115, 90))
sc.blit(ff, (210, 170))
sc.blit(res, (20, 170))
pygame.display.update()
# get event list
for event in pygame.event.get():
mouse_x, mouse_y = pygame. mouse. get_pos()
if event.type == pygame.QUIT:
pygame.quit() # exit window
sys.exit() # Exit the program
if event.type == pygame.MOUSEBUTTONDOWN:
# The effect after clicking the pause button
if 115 < mouse_x < 185 and 90 < mouse_y < 160:
btn_flag += 1
# 1. Click the next song button to switch to the next song
if 210 < mouse_x < 280 and 90 < mouse_y < 160:
# index + =1 #Try simply, click the next song several times, and find that the list index exceeds the limit.
index = (index + 1) % len(song_list) # realize music carousel
song = song_list[index]
pygame.mixer.music.load(song)
pygame.mixer.music.play(1, 0)
# 1. Click the previous button to switch to the previous music
if 20 < mouse_x < 90 and 90 < mouse_y < 160:
# index + =1 #Try simply, click the next song several times, and find that the list index exceeds the limit.
index = (index + length - 1) % length # realize music carousel
song = song_list[index]
pygame.mixer.music.load(song)
pygame.mixer.music.play(1, 0)
# 2. The effect after clicking the fast forward
elif 210 < mouse_x < 280 and 170 < mouse_y < 240:
# Get the playback time and convert it to seconds
# Time is added automatically
time += 10
print(time)
pygame.mixer.music.play(1, time)
print(time)
# Advance knowledge
if pygame.mixer.music.get_busy() == False:
# Get the new subscript
index = (index + 1) % length
# Play new music
song = song_list[index]
pygame.mixer.music.load(song)
pygame.mixer.music.play()
# 3. Click on the effect after replaying
elif 20 < mouse_x < 90 and 170 < mouse_y < 240:
time = 0
pygame.mixer.music.play(1, time)
# 4. The effect after clicking random
elif 115 < mouse_x < 185 and 170 < mouse_y < 240:
# Get the new subscript
index = random.randint(0, 3)
# Play new music
song = song_list[index]
pygame.mixer.music.load(song)
pygame.mixer.music.play()
Hope it helps everyone
A little programmer dedicated to office automation#
I have seen this, follow + like + bookmark = don’t get lost! !
If you want to know more about Python office automation, please pay attention!