简体   繁体   English

如何在 pygame 中只播放一个音乐文件?

[英]How to play only one music file in pygame?

I've loaded 2 music files as a wav into my program, but when I do pygame.mixer.music.play(-1) it plays both over the top of each other, but I only want one to play, and then the other at another time.我已经将 2 个音乐文件作为 wav 加载到我的程序中,但是当我执行 pygame.mixer.music.play(-1) 时,它会相互叠加播放,但我只想播放一个,然后另一个在另一个时间。

I would imagine there would be a way to "unload" the music or something, but not sure, so any methods are greatly appreciated!我想有一种方法可以“卸载”音乐或其他东西,但不确定,所以任何方法都非常感谢!

According to pygame docs ( https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load ), you can use these methods to load, unload and play musicfiles:根据 pygame 文档( https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.html#pygame.mixer.music.html#pygame.mixer.music . 可以使用这些方法加载和卸载音乐)

// Load musicfile
pygame.mixer.music.load(filename)

// Unload current musicfile
pygame.mixer.music.unload()

// Play current loaded musicfile
pygame.mixer.music.play()

Make a method that takes a musicfile as a parameter, load it and play it, eg:创建一个以音乐文件为参数的方法,加载并播放它,例如:

def play_music(filename)
    // Unload the current loaded music file
    pygame.mixer.music.unload()

    pygame.mixer.music.load(filename)
    pygame.mixer.music.play()

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

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