繁体   English   中英

python - pygame 音频播放器。 获取歌曲的结尾以播放下一首歌曲

[英]python - pygame audio player. Get end of the song to play the next in line

我被我的 python 音频播放器困住了。 我想要做的是在歌曲结束时获取事件,以便我可以播放下一首歌曲。 我可以切换播放列表文件夹并播放一首歌曲。 但是我怎样才能切换到下一个?

这是代码:

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

我真的希望有人能帮忙。 提前致谢...

我也有 pygame.mixer.music.queue 的错误,但您可以使用以下类:

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

如下

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

这对我有用

使用pygame.mixer.music.queue

它排队一首歌曲,将在第一首歌曲播放完毕后开始播放:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM