-1

Trying to make a playlist maker using pygame but it will only play the very first song on the list and it does load the next song but no audio is played

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)

1 Answers1

0

I think you could try this code:

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())

You can also watch the following links:

Hope this is helpful.

BlandCorporation
  • 1,324
  • 1
  • 15
  • 33
Toars
  • 19
  • 2