Pygam Lesson 5 - Homemade Music Player

Foreword: We have learned a lot of knowledge points in the first few lessons, welcome everyone to go to archaeology

insert image description here

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)

Please add a picture description

analyze

  • load music

pygame.mixer.music.load(song) # 加载音乐
pygame.mixer.music.set_volume(0.5) # 设置播放器音量,参数:0-1,切记写小一点,以免声音过大
  • play

'''
第一个参数1指播放一次,0指循环播放
time:指从第几毫秒开始播放
 pygame.mixer.music.play(1, time)
'''
pygame.mixer.music.play() # 播放
  • pause

pygame.mixer.music.pause()
  • resume playback

pygame.mixer.music.unpause()
  • fast forward

'''
第一个参数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

insert image description here

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,控制暂停/开始按钮的形态
btn_flag = 0
# 9把歌曲名字保存在列表,客户端加密后的名字很长,最好把每首歌名换行并对齐,方便查阅
song_list = ['./music/'+i for i in os.listdir('./music/')]

# 载入音乐
index = 0
song = song_list[index]
pygame.mixer.music.load(song)
# 控制音量,取值范围为0-1.0的浮点数。0为最小值,1为最大值。
pygame.mixer.music.set_volume(0.5)
# 播放第一首音乐,此处提醒学生,调小电脑音量,防止声音突然太大伤害听力
pygame.mixer.music.play()
time = 0
print(time)
length = len(song_list)
while True:  # 加上循环,引出事件

    # 如果是偶数,按钮切换为“start”,如果时奇数,切换为“stop”。
    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
            # 1. 点击下一曲按钮,切换为下一首音乐
            if 210 < mouse_x < 280 and 90 < mouse_y < 160:
                # index+=1 #简单尝试,多点几次下一曲,发现列表索引超限。
                index = (index + 1) % len(song_list)  # 实现音乐的轮播
                song = song_list[index]
                pygame.mixer.music.load(song)
                pygame.mixer.music.play(1, 0)
            # 1. 点击上一曲按钮,切换为上一首音乐
            if 20 < mouse_x < 90 and 90 < mouse_y < 160:
                # index+=1 #简单尝试,多点几次下一曲,发现列表索引超限。

                index = (index + length - 1) % length  # 实现音乐的轮播
                song = song_list[index]
                pygame.mixer.music.load(song)
                pygame.mixer.music.play(1, 0)
            # 2. 点击了快进后的效果
            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()

            # 3. 点击了重新播放后的效果
            elif 20 < mouse_x < 90 and 170 < mouse_y < 240:
                time = 0
                pygame.mixer.music.play(1, time)

            # 4. 点击了随机后的效果
            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!

Guess you like

Origin blog.csdn.net/weixin_42636075/article/details/130811078