Reputation: 333
I'm stuck with my python audio player. What I want to do is to get the event when the song is over, so that I can play the next song in line. I can switch the playlist folder and play one song. But how can I switch to the next one?
Here is the code:
import pygame
from pygame.locals import *
import os
import sys
import random
import re
play_stereo = True
songNumber = 0
x = 0
SONG_END = pygame.USEREVENT + 1
pygame.display.set_caption("Pygame Audio player")
screen = pygame.display.set_mode((800, 800), 0, 32)
pygame.init()
def build_file_list():
file_list = []
for root, folders, files in os.walk(folderPath):
folders.sort()
files.sort()
for filename in files:
if re.search(".(aac|mp3|wav|flac|m4a|pls|m3u)$", filename) != None:
file_list.append(os.path.join(root, filename))
return file_list
def play_songs(file_list):
random.shuffle(file_list)
pygame.mixer.music.load(file_list[songNumber])
pygame.mixer.music.play(1)
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_q:
folderPath = "/Users/dimitristeinel/Documents/git projects/pygame audioplayer/playlist1"
files = build_file_list()
play_songs(files)
if event.key == K_w:
folderPath = "/Users/dimitristeinel/Documents/git projects/pygame audioplayer/playlist2"
files = build_file_list()
print(files)
if event.key == K_ESCAPE:
sys.exit()
break
I really hope someone can help. Thanks in advance...
Upvotes: 0
Views: 4546
Reputation: 57
I have also had errors with pygame.mixer.music.queue, but you can use the following class:
import pygame
class PlayList:
playlist_on_play = None
def __init__(self, list_files):
self.list_files = list_files
self.song_num = 0
self.reproducing = False
def play(self):
if PlayList.playlist_on_play is not None:
PlayList.playlist_on_play.stop()
PlayList.playlist_on_play = self
def stop(self):
self.reproducing = False
self.song_num = 0
if PlayList.playlist_on_play is self:
PlayList.playlist_on_play = None
@staticmethod
def check(event):
if PlayList.playlist_on_play is not None:
pl = PlayList.playlist_on_play
if not pl.reproducing: # play first song
pl.play_song(pl.list_files[pl.song_num])
pl.reproducing = True
if event.type == MUSIC_END and pl.song_num < len(pl.list_files)-1: # play rest of songs
pl.song_num += 1
pl.play_song(pl.list_files[pl.song_num])
if event.type == MUSIC_END and pl.song_num == len(pl.list_files)-1: # end playlist
pl.stop()
print("end playlist")
def play_song(self, file):
pygame.mixer.music.load(file)
pygame.mixer.music.play()
as follows
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((400, 300))
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
list_1 = ["a2.mp3", "a2.mp3", "a2.mp3"]
list_2 = ["a3.mp3", "a3.mp3", "a3.mp3"]
playlist1 = PlayList(list_1)
playlist2 = PlayList(list_2)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
PlayList.check(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
print("playlist 1")
playlist1.play()
if event.key == pygame.K_w:
print("playlist 2")
playlist2.play()
if event.key == pygame.K_ESCAPE:
break
pygame.quit()
that worked for me
Upvotes: 1
Reputation: 64258
Use pygame.mixer.music.queue
.
It queues up a song which will begin playing after the first one is finished:
def play_songs(file_list):
random.shuffle(file_list)
pygame.mixer.music.load(file_list[songNumber])
pygame.mixer.music.play(1)
for num, song in enumerate(file_list):
if num == songNumber:
continue # already playing
pygame.mixer.music.queue(song)
Upvotes: 1