Foreword: We have learned a lot of knowledge points in the first few lessons, welcome everyone to go to archaeology
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)
analyze
pygame. mixer. music. load( song)
pygame. mixer. music. set_volume( 0.5 )
'''
第一个参数1指播放一次,0指循环播放
time:指从第几毫秒开始播放
pygame.mixer.music.play(1, time)
'''
pygame. mixer. music. play( )
pygame. mixer. music. pause( )
pygame. mixer. music. unpause( )
'''
第一个参数1指播放一次,0指循环播放
time:指从第几毫秒开始播放
pygame.mixer.music.play(1, time)
'''
pygame. mixer. music. play( 1 , time)
Shuffle Playback
randon.randint(number of music)
Matters needing attention 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
effect
image2
Image storage of function icons on the player interface
music
store music
the cover
Store Music Covers
Full version code:
import os
import pygame, random, sys, time
pygame. init( )
sc = pygame. display. set_mode( ( 300 , 250 ) )
bg = pygame. image. load( "image2/bg.png" )
pre_song = pygame. image. load( "image2/上一首.png" )
start = pygame. image. load( "image2/播放.png" )
pause = pygame. image. load( "image2/暂停.png" )
next_song = pygame. image. load( "image2/下一首.png" )
ff = pygame. image. load( "image2/快进.png" )
res = pygame. image. load( "image2/重播.png" )
rand = pygame. image. load( "image2/随机播放.png" )
bg_list = os. listdir( "./封面/" )
bg_list1 = [ ]
for bg in bg_list:
bg_list1. append( pygame. image. load( "./封面/" + bg) )
btn = start
btn_flag = 0
song_list = [ './music/' + i for i in os. listdir( './music/' ) ]
index = 0
song = song_list[ index]
pygame. mixer. music. load( song)
pygame. mixer. music. set_volume( 0.5 )
pygame. mixer. music. play( )
time = 0
print ( time)
length = len ( song_list)
while True :
if btn_flag % 2 == 0 :
btn = start
pygame. mixer. music. pause( )
else :
btn = pause
pygame. mixer. music. unpause( )
sc. blit( bg_list1[ index] , ( 0 , 0 ) )
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( )
for event in pygame. event. get( ) :
mouse_x, mouse_y = pygame. mouse. get_pos( )
if event. type == pygame. QUIT:
pygame. quit( )
sys. exit( )
if event. type == pygame. MOUSEBUTTONDOWN:
if 115 < mouse_x < 185 and 90 < mouse_y < 160 :
btn_flag += 1
if 210 < mouse_x < 280 and 90 < mouse_y < 160 :
index = ( index + 1 ) % len ( song_list)
song = song_list[ index]
pygame. mixer. music. load( song)
pygame. mixer. music. play( 1 , 0 )
if 20 < mouse_x < 90 and 90 < mouse_y < 160 :
index = ( index + length - 1 ) % length
song = song_list[ index]
pygame. mixer. music. load( song)
pygame. mixer. music. play( 1 , 0 )
elif 210 < mouse_x < 280 and 170 < mouse_y < 240 :
time += 10
print ( time)
pygame. mixer. music. play( 1 , time)
print ( time)
if pygame. mixer. music. get_busy( ) == False :
index = ( index + 1 ) % length
song = song_list[ index]
pygame. mixer. music. load( song)
pygame. mixer. music. play( )
elif 20 < mouse_x < 90 and 170 < mouse_y < 240 :
time = 0
pygame. mixer. music. play( 1 , time)
elif 115 < mouse_x < 185 and 170 < mouse_y < 240 :
index = random. randint( 0 , 3 )
song = song_list[ index]
pygame. mixer. music. load( song)
pygame. mixer. music. play( )
I hope everyone has to help
A little programmer dedicated to office automation#
I've seen this, follow + like + bookmark = don't get lost! !
If you want to know more about Python office automation, please pay attention!