简体   繁体   English

如何在PyGame中一次播放多首歌曲?

[英]How can I play more than one song at a time in PyGame?

I've got it working now but with the time delay is there a better way because I want two different scripts to be working I want to have these playing in this order and have my images come up in order and the images are a long script and have time delays on them too. 我现在已经有了它的工作但是时间延迟有一个更好的方法,因为我想要两个不同的脚本工作我希望这些按顺序播放并让我的图像按顺序出现并且图像是一个很长的脚本他们也有时间延迟。

#!/usr/bin/env python
import pygame
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
print "hey I finaly got this working!"
sounda= pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG')
soundb= pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG')
soundc= pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG')
soundd= pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG')
sounda.play()
pygame.time.delay(11000)
soundb.play()<P>
pygame.time.delay(180000)
soundc.play()
pygame.time.delay(90000)
soundd.play()

Did you check the pygame.Mixer module ? 你检查了pygame.Mixer模块吗? On default, you can play 8 songs simultaneously 默认情况下,您可以同时播放8首歌曲

If you use the pygame.mixer.music , you'll be able to play only one song at the time. 如果您使用pygame.mixer.music ,那么您当时只能播放一首歌曲。

If you use the pygame.mixer.sound , you'll be able to play up to 8 songs at the time. 如果你使用pygame.mixer.sound ,那么你当时最多可以播放8首歌曲。

The music module is here to stream music (it doesn't load all the music file at once). 音乐模块用于流式传输音乐(它不会立即加载所有音乐文件)。

The sound module is here to play differents sounds during game (sounds are completely loaded in memory). 声音模块在这里播放不同的声音(声音在内存中完全加载)。

So, in your example if you want to play the 4 songs at the same time : 因此,在您的示例中,如果您想同时播放4首歌曲:

import pygame
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
print "hey I finaly got this working!"
sounds = []
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/FUN.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/Still Alive.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/turret.OGG'))
sounds.append(pygame.mixer.Sound('D:/Users/John/Music/Music/portalend.OGG'))
for sound in sounds:
    sound.play()

The following script will load 4 sounds (sound_0.wav to sound_3.wav) and play them. 以下脚本将加载4个声音(sound_0.wav到sound_3.wav)并播放它们。

sounds = []
for i in range(4):
    sound = pygame.mixer.Sound('sound_%d.wav'%i)
    sound.play()
    sounds.append(sound)

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

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