使用 pygame.mixer 创建播放列表 [英] Create a playlist using pygame.mixer

查看:62
本文介绍了使用 pygame.mixer 创建播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一个简单的播放列表,一个接一个播放 3 首歌曲,下面的代码显示了这种尝试.它不会播放第一首歌曲 music1.mp3,而是播放最后一首歌曲 music3.mp3 并停止播放.我曾尝试查看类似的问题,但一直在努力寻找解决方案.

I have been trying to make a simple playlist that plays 3 songs one after the other the code below shows an attempt of that. Rather than playing the first song music1.mp3 it plays the last song music3.mp3 and stops playing. I have tried looking at similar questions but strugling to find a soloution.

如果有人可以帮助我提供一些建议或指导,我们将不胜感激.

Please if anyone can help give me some advice or some guidance it will be much appreciated.

迈克

import pygame
import time

pygame.mixer.init()
pygame.display.init()

screen = pygame.display.set_mode ( ( 420 , 240 ) )

playlist = list()
playlist.append ( "music1.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music3.mp3" )

pygame.mixer.music.load ( playlist.pop() )  # Get the first track from the playlist
pygame.mixer.music.queue ( playlist.pop() ) # Queue the 2nd song
pygame.mixer.music.set_endevent ( pygame.USEREVENT )    # Setup the end track event
pygame.mixer.music.play()           # Play the music

running = True
while running:
   for event in pygame.event.get():
      if event.type == pygame.USEREVENT:    # A track has ended
         if len ( playlist ) > 0:       # If there are more tracks in the queue...
            pygame.mixer.music.queue ( playlist.pop() ) # Queue the next one in the list

推荐答案

list.pop() 返回列表中的最后一项.如果你改变了:

list.pop() returns the last item in the list. If you changed:

playlist.append ( "music1.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music3.mp3" )

playlist.append ( "music3.mp3" )
playlist.append ( "music2.mp3" )
playlist.append ( "music1.mp3" )

然后它会正常工作.

这篇关于使用 pygame.mixer 创建播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆