尝试使用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)
答案 0 :(得分: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())
您还可以观看以下链接:
希望这有用。