-1

尝试使用 pygame 制作播放列表制作器,但它只会播放列表中的第一首歌曲,它会加载下一首歌曲但不播放音频

from pygame import mixer # Load the required library
from os import listdir
from mutagen.mp3 import MP3
import time
k = listdir('C:/LOCAL')
print(k)
mixer.init()
for x in k:
   y = "C:/LOCAL/" + x
   print y
   mixer.music.load(y)
   mixer.music.play()
   tracklength = MP3(y).info.length
   print tracklength
   time.sleep(tracklength)
4

1 回答 1

0

我想你可以试试这个代码:

import time
import pygame
from os import listdir

pygame.mixer.init()
tracks = listdir('C:/LOCAL')

# Create the playlist.
playlist = list()
for track in tracks:
    playlist.append(track)

# Define the song end event.
SONG_END = pygame.endevent.USEREVENT + 1

pygame.mixer.music.load(playlist.pop())   # Load the first track to play.
pygame.mixer.music.queue(playlist.pop())  # Add the second track in queue.
pygame.mixer.music.set_endevent(SONG_END) # Set the event.
pygame.mixer.music.play()                 # Play.

while True:
    for event in pygame.event.get():
        if event.type == SONG_END: # A track has ended.
            if len(playlist) > 0:  # Queue the next track if there is one.
                pygame.mixer.music.queue(playlist.pop())

您还可以观看以下链接:

希望这会有所帮助。

于 2016-05-11T10:07:29.520 回答