import pygame,sys
pygame.init()
screen = pygame.display.set_mode((800,500))
pygame.display.set_caption("音乐播放器")
在第三行的 pygame.display.set_mode((800,500) ) 中可以设置窗口大小
soundList = ["xxxxx.mp3","xxxx.mp3","xxx.mp3","xx.mp3","x.mp3"]
f = 0
songNum = 0
pygame.mixer.music.load(soundList[songNum])
把音乐名称输入到列表中,用 pygame.mixer.music.load(soundList[songNum]) 加载第一首歌
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
#——————————————————————————————
#键盘事件控制播放、停止、暂停、继续播放
#键盘事件控制上一曲、下一曲
if event.key == pygame.K_LEFT:
songNum = songNum -1#切换歌曲步骤一
if songNum < 0:
songNum = len(soundList)-1
pygame.mixer.music.load(soundList[songNum])#切换歌曲步骤二
pygame.mixer.music.play()#播放歌曲
elif event.key == pygame.K_RIGHT:
songNum = songNum +1
if songNum > len(soundList)-1:
songNum = 0
pygame.mixer.music.load(soundList[songNum])
pygame.mixer.music.play()#播放歌曲
elif event.key == pygame.K_RETURN:
pygame.mixer.music.play()#播放歌曲
elif event.key == pygame.K_SPACE and f == 1:
pygame.mixer.music.pause()#暂停
f = 0
elif event.key == pygame.K_SPACE and f == 0:
pygame.mixer.music.unpause()#继续播放
f = 1
elif event.key == pygame.K_s:
pygame.mixer.music.stop()
#————————————————————————————————