如何在第一首歌曲结束后安排音频文件在 pygame 中自动播放? [英] How do I schedule an audio file to play automatically in pygame after the first song ends?

查看:86
本文介绍了如何在第一首歌曲结束后安排音频文件在 pygame 中自动播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用队列功能,但是

I tried using the queue function, but

pygame.mixer.music.queue(filename) 

似乎不起作用.

这是我用来运行我的 mp3 文件的代码:

Here is the code I use to run my mp3 file:

def playmusic(self):
    pygame.mixer.init()
    pygame.mixer.music.load(self.music_link+self.files[self.file_index])
    pygame.mixer.music.play()
    self.pausedmusic = 0
    self.file_index = self.fileindex + 1

    pygame.mixer.music.queue(self.music_link+self.files[self.file_index])

我尝试使用事件,但也没有得到解决方案.

I tried to use events but got no solution from it either.

如果我使用此代码,

while(pygame.mixer.music.get_busy()):
    continue
self.playmusic()

Tkinter GUI 没有响应,但歌曲一直在播放,它也会自动播放下一首歌曲,让我的播放器在播放完所有歌曲之前都没有响应.

the Tkinter GUI is unresponsive but the song keeps playing and it plays the next song automatically, too, keeping my player unresponsive till all songs are played.

我使用的是 Python 3.6.

I'm using Python 3.6.

推荐答案

将您的音乐文件(路径)放入列表,定义自定义用户事件并调用 pygame.mixer.music.set_endevent(YOUR_USEREVENT).然后 pygame 会在歌曲完成时将此事件添加到事件队列中,您可以执行一些代码来更改当前歌曲的索引.在下面的示例中,您可以通过按向右箭头键来增加索引,也可以等到歌曲完成(发出 SONG_FINISHED 事件),然后程序将选择随机歌曲(索引).

Put your music files (paths) into a list, define a custom userevent and call pygame.mixer.music.set_endevent(YOUR_USEREVENT). Then pygame will add this event to the event queue when a song is finished and you can execute some code to change the index of the current song. In the example below you can either increment the index by pressing the right arrow key or wait until a song is finished (the SONG_FINISHED event is emitted) and the program will choose a random song (index).

import random
import pygame as pg


pg.mixer.pre_init(44100, -16, 2, 2048)
pg.init()
screen = pg.display.set_mode((640, 480))

# A list of the music file paths.
SONGS = ['file1.ogg', 'file2.ogg', 'file3.ogg']
# Here we create a custom event type (it's just an int).
SONG_FINISHED = pg.USEREVENT + 1
# When a song is finished, pygame will add the
# SONG_FINISHED event to the event queue.
pg.mixer.music.set_endevent(SONG_FINISHED)
# Load and play the first song.
pg.mixer.music.load('file1.ogg')
pg.mixer.music.play(0)


def main():
    clock = pg.time.Clock()
    song_idx = 0  # The index of the current song.
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                # Press right arrow key to increment the
                # song index. Modulo is needed to keep
                # the index in the correct range.
                if event.key == pg.K_RIGHT:
                    print('Next song.')
                    song_idx += 1
                    song_idx %= len(SONGS)
                    pg.mixer.music.load(SONGS[song_idx])
                    pg.mixer.music.play(0)
            # When a song ends the SONG_FINISHED event is emitted.
            # Then just pick a random song and play it.
            elif event.type == SONG_FINISHED:
                print('Song finished. Playing random song.')
                pg.mixer.music.load(random.choice(SONGS))
                pg.mixer.music.play(0)

        screen.fill((30, 60, 80))
        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()

这篇关于如何在第一首歌曲结束后安排音频文件在 pygame 中自动播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆